Reputation: 1158
I'm trying to dynamically add radio buttons from code behind. I add it by calling the following code:
private void AddRadioButtonList(string id, bool isBool)
{
RadioButtonList radioButtonList = new RadioButtonList();
radioButtonList.ID = id;
form1.Controls.Add(radioButtonList);
if (isBool) { GenerteTrueFalseListItems(radioButtonList); }
form1.Controls.Add(new LiteralControl("<br />"));
}
When isBool
is true, the following function is called:
private void GenerteTrueFalseListItems(RadioButtonList item)
{
item.Items.Clear();
item.Items.Add(new ListItem("True", "true"));
item.Items.Add(new ListItem("False", "false"));
}
But radio buttons on page are missing correct IDs, and I can select only one radio button from whole page.
I am expecting the IDs of the radio buttons to be either "true" or "false".
What should I do in order to have radio buttons rendered correctly?
Upvotes: 0
Views: 2947
Reputation: 10772
The value
of the input
element will be true
or false
but the ID is automatically generated by ASP.NET based on the ID you give the RadioButtonList (not the ListItem) and then an underscore and then a sequential number for each RadioButtonList option. (Will vary based on ClientIDMode
setting).
So if you assign an ID of test
to a RadioButtonList then the input
elements that ASP.NET generates for each ListItem
will have the ID test_1
, test_2
, test_3
, etc etc.
If you were to change to RadioButtons instead of a RadioButtonList then each RadioButton would have the ID you specify (depending on ClientIDMode
).
Upvotes: 1