Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > how to get selectedvalue of radiobuttonlist in Javascript?

Reply
Thread Tools

how to get selectedvalue of radiobuttonlist in Javascript?

 
 
Arnold
Guest
Posts: n/a
 
      11-12-2006
Hi,

I need the SelectedValue of a radiobuttonlist in a Javascript variable. The
radiobuttonlist is only used in a form for inputting data into a database.
Before sending it to the database, i check the inputted values in
Javascript.
(By the way there is also a dropdownlist in the form, and i have no problem
with getting its SelectedValue with the same javascript code). With the
radiobuttonlist, i get "undefined" as value.

Thanks for helping me
Arnold

The radiobuttonlist is created in the code-behind like this:

Dim rb As RadioButtonList
Dim frm As HtmlForm = Me.FindControl("form1")
Dim rbl(2) As ListItem
rb = New RadioButtonList
rbl(1) = New ListItem("option 1", 1)
rb.Items.Add(rbl(1))
rbl(2) = New ListItem("option 2", 2).
rb.Items.Add(rbl(2))
rb.ID = "radio1"
frm.Controls.Add(rb)

The code in the aspx file:
<form id="form1" runat="server">
<input id="Sub1" type="button" value="submit" onclick="checkvalue()"/>
</form>
<script type="text/javascript">
var antw
function checkvalue()
{
antw=document.getElementById("radio1").value
alert(antw) // this gives: undefined
.....
.....
}
</script>




 
Reply With Quote
 
 
 
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      11-12-2006
Arnold wrote:
> Hi,
>
> I need the SelectedValue of a radiobuttonlist in a Javascript
> variable. The radiobuttonlist is only used in a form for inputting
> data into a database. Before sending it to the database, i check the
> inputted values in Javascript.
> (By the way there is also a dropdownlist in the form, and i have no
> problem with getting its SelectedValue with the same javascript
> code). With the radiobuttonlist, i get "undefined" as value.
>
> Thanks for helping me
> Arnold
>
> The radiobuttonlist is created in the code-behind like this:
>
> Dim rb As RadioButtonList
> Dim frm As HtmlForm = Me.FindControl("form1")
> Dim rbl(2) As ListItem
> rb = New RadioButtonList
> rbl(1) = New ListItem("option 1", 1)
> rb.Items.Add(rbl(1))
> rbl(2) = New ListItem("option 2", 2).
> rb.Items.Add(rbl(2))
> rb.ID = "radio1"
> frm.Controls.Add(rb)
>
> The code in the aspx file:
> <form id="form1" runat="server">
> <input id="Sub1" type="button" value="submit" onclick="checkvalue()"/>
> </form>
> <script type="text/javascript">
> var antw
> function checkvalue()
> {
> antw=document.getElementById("radio1").value
> alert(antw) // this gives: undefined
> ....
> ....
> }
> </script>



Run the page then View Source to see the radio buttons generated by the .Net
control. This will reveal their IDs at which point it should be a simple
matter to to use getElementById to get the value.

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


 
Reply With Quote
 
 
 
 
johns221b
Guest
Posts: n/a
 
      11-12-2006


"Arnold" wrote:

> Hi,
>
> I need the SelectedValue of a radiobuttonlist in a Javascript variable. The
> radiobuttonlist is only used in a form for inputting data into a database.
> Before sending it to the database, i check the inputted values in
> Javascript.
> (By the way there is also a dropdownlist in the form, and i have no problem
> with getting its SelectedValue with the same javascript code). With the
> radiobuttonlist, i get "undefined" as value.
>
> Thanks for helping me
> Arnold


RadionList value can be processed in server side before sending to database.
radio1.SelectedItem.Text
For javascript you will have to loop through the collection of radio
buttons and check whether its checked or not, then take cchecked value.

thanks,
John Chacko





>
> The radiobuttonlist is created in the code-behind like this:
>
> Dim rb As RadioButtonList
> Dim frm As HtmlForm = Me.FindControl("form1")
> Dim rbl(2) As ListItem
> rb = New RadioButtonList
> rbl(1) = New ListItem("option 1", 1)
> rb.Items.Add(rbl(1))
> rbl(2) = New ListItem("option 2", 2).
> rb.Items.Add(rbl(2))
> rb.ID = "radio1"
> frm.Controls.Add(rb)
>
> The code in the aspx file:
> <form id="form1" runat="server">
> <input id="Sub1" type="button" value="submit" onclick="checkvalue()"/>
> </form>
> <script type="text/javascript">
> var antw
> function checkvalue()
> {
> antw=document.getElementById("radio1").value
> alert(antw) // this gives: undefined
> .....
> .....
> }
> </script>
>
>
>
>
>

 
Reply With Quote
 
MikeS
Guest
Posts: n/a
 
      11-12-2006
Since a RadioButton list doesn't have a OnClientClick property you can
add something like it and save the selected value.

<%@ Page Language="VB" %>

<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
InitList()
End Sub
Private Sub InitList()
For Each li As ListItem In RadioButtonList1.Items
li.Attributes.Add("onclick", "rb_click(this)")
Next
TextBox1.Text = RadioButtonList1.SelectedValue
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>

<script type="text/javascript">
function rb_click(el) {
document.getElementById("<%=TextBox1.ClientID%>"). value =
el.value;
}
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br
/>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Selected="True">A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
</asp:RadioButtonList>
<asp:Button ID="Button1" runat="server"
OnClick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>

 
Reply With Quote
 
Arnold
Guest
Posts: n/a
 
      11-12-2006
Thanks to all

"Arnold" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed)...
> Hi,
>
> I need the SelectedValue of a radiobuttonlist in a Javascript variable.
> The radiobuttonlist is only used in a form for inputting data into a
> database. Before sending it to the database, i check the inputted values
> in Javascript.
> (By the way there is also a dropdownlist in the form, and i have no
> problem with getting its SelectedValue with the same javascript code).
> With the radiobuttonlist, i get "undefined" as value.
>
> Thanks for helping me
> Arnold
>
> The radiobuttonlist is created in the code-behind like this:
>
> Dim rb As RadioButtonList
> Dim frm As HtmlForm = Me.FindControl("form1")
> Dim rbl(2) As ListItem
> rb = New RadioButtonList
> rbl(1) = New ListItem("option 1", 1)
> rb.Items.Add(rbl(1))
> rbl(2) = New ListItem("option 2", 2).
> rb.Items.Add(rbl(2))
> rb.ID = "radio1"
> frm.Controls.Add(rb)
>
> The code in the aspx file:
> <form id="form1" runat="server">
> <input id="Sub1" type="button" value="submit" onclick="checkvalue()"/>
> </form>
> <script type="text/javascript">
> var antw
> function checkvalue()
> {
> antw=document.getElementById("radio1").value
> alert(antw) // this gives: undefined
> ....
> ....
> }
> </script>
>
>
>
>



 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
trouble converting RadioButtonList.SelectedValue into a string Jeff ASP .Net 2 10-08-2008 12:45 AM
how to get selectedvalue of radiobuttonlist in Javascript? Arnold ASP .Net Web Controls 6 11-12-2006 05:36 PM
RadioButtonList SelectedValue thinkmast ASP .Net 0 05-07-2006 08:21 PM
How do I get the SelectedValue from a RadioButtonList using javascript pierre@globalkinetic.net ASP .Net Web Controls 1 03-29-2005 02:34 PM



Advertisments