views:

2737

answers:

3

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

+3  A: 

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.

rocka
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?
Andi
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.
womp
@SocialAddict When you view the RadioButtons as they are rendered IN HTML what are the IDs set to?
rocka
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?
Andi
@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>
Andi
That depends on where you're process selection. On the server side or client side?
rocka
@rocka on the server side
Andi
Are you already handling some kind of postback event not related to the Radio Buttons? If not you could register for CheckChanged event on each of the RadioButtons as you create them. The on the CheckChanged handler cast the sender as a RadioButton. Then you can determine the GroupName and the RadioButtons's Checked status.
rocka
@rocka This is on a entry page to the system and i need them to select an appointment type from a list of radioButtons. The system has to work without javascript too
Andi
Where you able to get a reference to the RadioButtons on the server side?
rocka
Thats the problem i just want to access only the selected value like i would if i posted a standard radio list in any language $_POST['myRadioInput'] . I'm just no wiser on how to retrieve it
Andi
+2  A: 

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.

Tim Saunders
'System.Web.UI.WebControls.RadioButton' does not contain a definition for 'Value'
Andi
Whooops! Oh the shame!
Tim Saunders
Corrected my brain and fingers not in sync error
Tim Saunders
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?
Andi
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.
Tim Saunders
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 :)
Andi
A: 

hi , this help me a lot , i just wanted to tnx you

sfsdf