473,387 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Javascript Function w/ webcontrol as a pramater

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
Nov 18 '05 #1
5 1910
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" <jf***********@hotmail.com> wrote in message
news:ef**************************@posting.google.c 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

Nov 18 '05 #2
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?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #3
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;
Nov 18 '05 #4
Stringbuilder is a .NET object, you can use it in VB.NET.

"Jon Turner" <jf***********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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?

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

Nov 18 '05 #5
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!
Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gijs Korremans | last post by:
Hi, One of the functions in the com object I need to use has a pointer in one of it's functions (object.function(string input, struct * output)) (I've created the struct with...
2
by: Peregrin | last post by:
Hello world. I will try to explain my problem. I've a webcontrol with an interface which provides a choice of methods. After including the webcontrol in a special aspx page, I want to call an...
3
by: Aamir Ghanchi | last post by:
Hi, I am wondering why the <%= % is not working when I use it to pass the clientid to a javascrip function from the aspx declaration code. Say, I have a listbox webcontrol and from its...
18
by: Dan | last post by:
Hi, There is a detailsview which contains a textbox in editmode. How to get the id of the textbox in javascript? I tried this: <asp:DetailsView ID="DetailsView1" runat="server" >...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.