Accessing value of dynamically created controls c# asp.net

23,555

Solution 1

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.

Solution 2

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.

Share:
23,555
satyavrat
Author by

satyavrat

Developer working with: Dynamics 365 SharePoint Azure - Cognitive Services | AI | ML Umbraco CMS .NET MVC

Updated on October 31, 2020

Comments

  • satyavrat
    satyavrat over 3 years

    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

  • satyavrat
    satyavrat over 14 years
    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?
  • nuiun
    nuiun over 14 years
    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.
  • rocka
    rocka over 14 years
    @SocialAddict When you view the RadioButtons as they are rendered IN HTML what are the IDs set to?
  • satyavrat
    satyavrat over 14 years
    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?
  • satyavrat
    satyavrat over 14 years
    @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>
  • rocka
    rocka over 14 years
    That depends on where you're process selection. On the server side or client side?
  • rocka
    rocka over 14 years
    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.
  • satyavrat
    satyavrat over 14 years
    'System.Web.UI.WebControls.RadioButton' does not contain a definition for 'Value'
  • satyavrat
    satyavrat over 14 years
    @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
  • rocka
    rocka over 14 years
    Where you able to get a reference to the RadioButtons on the server side?
  • satyavrat
    satyavrat over 14 years
    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
  • satyavrat
    satyavrat over 14 years
    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?
  • Tim
    Tim over 14 years
    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.
  • satyavrat
    satyavrat over 14 years
    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 :)