views:

22

answers:

2

I'm having some real problems with a group of HTML Radiobuttons when I try to pass the selected values back and forth between JavaScript and ASP.NET.

<input type="radio" id="radio1" name="markerSel" value="1" checked=true />
<input type="radio" id="radio2" name="markerSel" value="2" />
<input type="radio" id="radio3" name="markerSel" value="3" />

To begin with, the form loads with these radiobuttons and a user selects a value and submits the ASP.NET form. To get the value of the selected option on the server-side I have tried;

string selVal = Request.Form["markerSel"]; 
//always returns "1", regardless of the selection made.

And also tried ASP.NET's FindControl method (recursively too), and I just get null.

I want to be able to set the value of this radiobutton group via JavaScript and Asp.NET and be able to read from both environments.

Any help would be appreciated.

Thanks

+1  A: 

Is there a reason why you don't use ASP.NET radio buttons instead of <input type=radio />?

If you use

<asp:RadioButton id="radio1" runat="server" Checked="true" />
<asp:RadioButton id="radio2" runat="server" />
<asp:RadioButton id="radio3" runat="server" />

You will be able to access them server-side by doing this.radio1.Checked

If you want to access your elements via javascript, you can do a document.getElementById('radio1') and you will get the DOM element that way.

If you want to use jQuery, you can access the radio buttons with

$('#radio1')

If you want to test if the radio button is checked, you can use

if ($("#radio1").is(':checked')) {//...
Hugo Migneron
Thanks Hugo, I've tried to use asp:RadioButton. But for some reason JQuery UI didn't work well with that. It never looked right, but I have now sorted it using <%= checkedVal%> syntax within the HTML.
Sivakanesh
+1  A: 

hmmm... I would try changing the line to:

string selVal = Request.Form("markerSel"); 
Rob Cooney