Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing value of dynamically created controls c# asp.net

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

like image 482
Andrew Avatar asked Sep 28 '09 15:09

Andrew


2 Answers

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.

like image 91
Tim Avatar answered Oct 20 '22 20:10

Tim


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.

like image 3
rocka Avatar answered Oct 20 '22 21:10

rocka