Javascript Function w/ webcontrol as a pramater

  • Thread starter Thread starter Jon
  • Start date Start date
J

Jon

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
 
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)
 
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 asp:DropDownList.

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?
 
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.Value
s = s + "<br>"
}

Label1my1.Text = s;
 
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.Selected)
{
sValue = Svalue + check1.Items.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>
 
Back
Top