Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Javascript Function w/ webcontrol as a pramater

Reply
Thread Tools

Javascript Function w/ webcontrol as a pramater

 
 
Jon
Guest
Posts: n/a
 
      04-23-2004
I would like to pass a reference of a webcontrol into a javascript
function. Below I have a snippet of code. "me.selectedValue" returns
undefined. I was hoping that would return the value that the user has
selected in the radio list..

ps: this is my first post, be gentle

**aspx page**

<form id="Form1" method="post" runat="server">
<asp:RadioButtonList ID=radio1 Runat=server >
<asp:ListItem Value=1 >Option 1</asp:ListItem>
<asp:ListItem Value=2 >Option 2</asp:ListItem>
<asp:ListItem Value=3 >Option 3</asp:ListItem>
</asp:RadioButtonList>
</form>

**Code Behind**
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim strScript As String = "<script language=""JavaScript"">" &
vbCrLf
strScript += " function test1(me) " & vbCrLf
strScript += "{alert(me.SelectedValue); " & vbCrLf
strScript += "}</script>"

If (Not Page.IsClientScriptBlockRegistered("clientScript") )
Then
Page.RegisterClientScriptBlock("clientScript", strScript)
End If
End Sub

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
radio1.Attributes.Add("onclick", "test1(this)")
End Sub
 
Reply With Quote
 
 
 
 
Rick Spiewak
Guest
Posts: n/a
 
      04-23-2004
You have Me.Selected.Value inside quotes. You seem to be passing what you
would like to have as a variable as a string.

This might be clearer if you used a StringBuilder:

Imports System.Text
..
..
..

Dim sbldr As New StringBuilder
sbldr.Append("<script language=""JavaScript"">") :
sbldr.Append(vbCrLf)
sbldr.Append(" function test1(me) ") : sbldr.Append(vbCrLf)
sbldr.AppendFormat("{alert({0}); {1}}</script>", Me.SelectedValue,
vbCrLf)

"Jon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I would like to pass a reference of a webcontrol into a javascript
> function. Below I have a snippet of code. "me.selectedValue" returns
> undefined. I was hoping that would return the value that the user has
> selected in the radio list..
>
> ps: this is my first post, be gentle
>
> **aspx page**
>
> <form id="Form1" method="post" runat="server">
> <asp:RadioButtonList ID=radio1 Runat=server >
> <asp:ListItem Value=1 >Option 1</asp:ListItem>
> <asp:ListItem Value=2 >Option 2</asp:ListItem>
> <asp:ListItem Value=3 >Option 3</asp:ListItem>
> </asp:RadioButtonList>
> </form>
>
> **Code Behind**
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> Dim strScript As String = "<script language=""JavaScript"">" &
> vbCrLf
> strScript += " function test1(me) " & vbCrLf
> strScript += "{alert(me.SelectedValue); " & vbCrLf
> strScript += "}</script>"
>
> If (Not Page.IsClientScriptBlockRegistered("clientScript") )
> Then
> Page.RegisterClientScriptBlock("clientScript", strScript)
> End If
> End Sub
>
> Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles MyBase.PreRender
> radio1.Attributes.Add("onclick", "test1(this)")
> End Sub



 
Reply With Quote
 
 
 
 
Jon Turner
Guest
Posts: n/a
 
      04-23-2004
Thank you for your quick response

I am currently working with VB. Is stringBuilder a C# object?

The javascript is now working. me.ID yields the objects ID and me.Value
yeilds the value for asp:TextBox and aspropDownList.

How do you get the selected values for the RadioButtonList and
CheckBoxList controls? Is there an easy way to do this or do you have
to do some wierd looping through the generated HTML table?



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
=?Utf-8?B?UGV0ZXI=?=
Guest
Posts: n/a
 
      04-23-2004
You can retrieve selected values like this

String s = "Selected items:<br>"

for (int i=0; i < CheckBoxList1.Items.Count; i++)

if ( CheckBoxList1.Items[ i ].Selected )

// List the selected item
s = s + CheckBoxList1.Items[i].Value
s = s + "<br>"
}

Label1my1.Text = s;
 
Reply With Quote
 
Rick Spiewak
Guest
Posts: n/a
 
      04-23-2004
Stringbuilder is a .NET object, you can use it in VB.NET.

"Jon Turner" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Thank you for your quick response
>
> I am currently working with VB. Is stringBuilder a C# object?
>
> The javascript is now working. me.ID yields the objects ID and me.Value
> yeilds the value for asp:TextBox and aspropDownList.
>
> How do you get the selected values for the RadioButtonList and
> CheckBoxList controls? Is there an easy way to do this or do you have
> to do some wierd looping through the generated HTML table?
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



 
Reply With Quote
 
Jon Turner
Guest
Posts: n/a
 
      04-23-2004
Thank you Peter. I have tried your example, but can't seem to get it to
work. Below is a code snippet. I keep getting the error Message
"'check1.Items.Count' is null or not an object"

My goal is to get the sum total of the selected values. Then take that
number and perform a bitwise operation with another number. If returns
true - make a Div visible Else Hidden.


<script language="JavaScript">
function test1()
{
for ( i=0; i < check1.Items.Count; i++)
{
if (check1.Items[i].Selected)
{
sValue = Svalue + check1.Items[i].Value;
}
}
alert(sValue);
}
</script>

*****

<asp:CheckBoxList ID=check1 Runat=server onclick="test1();">
<asp:ListItem Selected=True Value=1>Option 1</asp:ListItem>
<asp:ListItem Value=2 >Option 2</asp:ListItem>
<asp:ListItem Value=4 >Option 3</asp:ListItem>
</asp:CheckBoxList>


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom webcontrol that references another webcontrol Fabricio ASP .Net Building Controls 0 09-21-2005 02:55 AM
return pramater in com object function Gijs Korremans Python 0 06-02-2005 02:33 PM
Trapping an Exception ocurring in a Webcontrol at the Page or parent WebControl level. ASP .Net 2 01-31-2005 06:34 PM
WebControl inside of WebControl and attaching events Kasabaarde Sumta ASP .Net Building Controls 0 12-13-2004 09:51 PM
Can a webcontrol (webcontrol.dll) have a configuration file? Luis Ramírez. ASP .Net Building Controls 2 07-06-2004 04:35 PM



Advertisments