tags:

views:

569

answers:

1

I've created a series of radio button controls in C#. (radCompany, radProperty etc.)
I've set their group name to be the same (282_Type) so they function as a list of radio buttons.

How do I retrieve the name (like: ct100$m$dfgadjkfasdghasdkjfg$282_Type) in c# so I can use this in a Javascript method i'm creating?

The values output are:
Radion Button 1
id="ct223423432243_radCompany" name="ct100$sdfsdf$sdfsdf$282_Type"
Radion Button 2
id="ct223423432243_radProperty" name="ct100$sdfsdf$sdfsdf$282_Type"

+3  A: 

You need to reference the ClientID of the control; this is the id in the final html.

Of course, another approach might be to use some other attribute (such as the css etc), and use jQuery to find it; jQuery takes a lot of DOM pain away from javascript. It is interesting that jQuery is now even supported by VS2008 (with intellisense etc).

I'm currently reading jQuery in Action, and liking it much. To take an example straight from the book (on the subject of radios):

var x = $('[name=radioGroup]:checked').val();

which returns the value of the single checked radio button in the group, or undefined if none is selected.

Re getting the name; it uses the internal UniqueGroupName property, which does a lot of mangling. An option (but not an attractive one) would be to use reflection to read UniqueGroupName. Alternatively, use something simple like a literal control. Gawd I hate the ASP.NET forms model... roll on MVC...

Fianlly - I'm currently looking at the public VS2010 CTP image; one of the new additions for ASP.NET is static ClientIDs, by setting ClientIdMode = Static on the control. A bit overdue, but not unwelcome.

Marc Gravell
(Deleted my suggestion of using ID so as not to distract from the correct answer.)
Jon Skeet
This does return the control id, however this is not the name of the list of radiobuttons so it returns the ID attribute rather then the name attribute which is what I want.
Bravax
I'll answer in the post...
Marc Gravell
Could you clarify what the name and id are coming down as? I'm not entirely sure what you are seeing...
Marc Gravell
Thanks for the suggestion however, I'm almost certainly not going to use jQuery for this, as this would add complexity unnessarily. An option is to not use c# created radiobuttons, just straight HTML ones.
Bravax
I've updated the question with the values being returned.
Bravax
more info updated
Marc Gravell
Given the ID, you can easily find the control in JavaScript and then get the name, surely.
Jon Skeet
@Jon - that would be easier and more supportable than reflection ;-p
Marc Gravell
I've just found that VS2010 makes things slightly easier (updated)
Marc Gravell
Thanks for your help guys. +1 to Jon. (I'm using your approach)
Bravax