6

I'm trying to access the value of a selected radioButton. I have a list of radio button's seperated by sub headers (hence why i havent used a radioList)

I don't know how to access the selected value as i'm not sure what name it's given with it being dynamic.

The following code is in a for loop and outputs radio options. All the options are for one required user selection.

//add list to radio button list
RadioButton button1 = new RadioButton { 
GroupName = "myGroup", 
Text = singleType.appTypeName, 
ID = singleType.appTypeID.ToString() 
};
radioArea.Controls.Add(button1);
myLabel = new Label();
myLabel.Text = "<br />";
radioArea.Controls.Add(myLabel);

This is how im trying to access it:

RadioButton myButton = (RadioButton) radioArea.FindControl("myGroup");

I realise this should be very simple but im still in a very much PHP mindset and appreciate any help.

Thanks

2 Answers 2

9

Hi it sounds to me like you are bit confused over what the various properties of the radio button control do and how to generate the radio button correctly.

The ID property is key here.

You should set the ID property to a unique string that you then use to access the control on postback.

The GroupName is just an alias for the standard name property of a radio button and is used so when a user clicks on a radio button in a group only one radio button can be selected.

For example:

//add list to radio button list
RadioButton radioButton = new RadioButton();
radioButton.GroupName = "radioGroup";
radioButton.Text = singleType.appTypeID.ToString();
radioButton.ID = singleType.appTypeName;

radioArea.Controls.Add(radioButton);

Label label = new Label();
label.Text = "<br />";
radioArea.Controls.Add(label);

In the above example I've assigned singleType.appTypeName as the ID, assuming that this is unique.

Then to retrive the value on postback do this, assumnig that singleType.appTypeName = "mySuperApp":

RadioButton radioButton = (RadioButton) radioArea.FindControl("mySuperApp");

Now you can access the radioButton variable to check which GroupName it has, get it's value and check that it is checked.

You will need to loop over the radio buttons to find out which one is checked. An easy way of doing this is looping over the child controls of the radioArea and checking eack control to see if it is a radio button and if it is checked. Another options is to give each ID a prefix i.e. ID="RAD_"+singleType.appTypeName and loop over the Request object and match each one with the correct suffix.

Sign up to request clarification or add additional context in comments.

4 Comments

'System.Web.UI.WebControls.RadioButton' does not contain a definition for 'Value'
surely if there all part of the same group i can just retreive the checked value rather than having to loop through loads of radio controls?
I'm afraid not. The GroupName attribute is just an alias for the normal name attribute on the radiobutton. The only way of doing what you would like is to use the RadioButtonList control. Otherwise it's just a case of looping through the controls to find the checked one.
ok doesnt really solve my problem so i've gone back to a dropdown and have themed it using jquery. +1 and answer all the same :)
3

The FindControl method you are using, expects you to pass it the ID that was assigned to control when you created it.

So for your example the RadioButton.ID should be equal to singleType.appTypeID.ToString()

Hope this helps.

10 Comments

when i view the source code of the code generated the singleType.appTypeID.ToString() is the value of each option not the ID. Any ideas?
Your code is assigning the ID of the radiobutton to be singleType.appTypeID.ToString(). You'll need to search for that exact ID in your FindControl() call.
@SocialAddict When you view the RadioButtons as they are rendered IN HTML what are the IDs set to?
All the radio buttons on the page are for one selection though. So i just need the selected value. If all the user controls have different ID's how do i just return the one from group myGroup thats selected?
@rocka <input id="ctl00_BodyContent_1" type="radio" name="ctl00$BodyContent$myGroup" value="1" /> <label for="ctl00_BodyContent_1">Single Birth</label> <input id="ctl00_BodyContent_2" type="radio" name="ctl00$BodyContent$myGroup" value="2" /> <label for="ctl00_BodyContent_2">Twin Birth</label>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.