My code-behind (c#) file declares RadioButtonLists dynamically for any given number of questions in my database using a while() loop:
I add items to each tmpRBL in a for loop.
I register each RadioButtonList to a child panel which I create at the beginning of each iteration of the while() loop.
I then add each panel to the parent panel.
while(reader.Read()
{
...
RadioButtonList tmpRBL = new RadioButtonList();
temp = "RadioButtonList" + count.ToString();
tmpRBL.ID = temp;
tmpRBL.RepeatDirection = System.Web.UI.WebControls.RepeatDirection.Vertical;
tmpRBL.TextAlign = System.Web.UI.WebControls.TextAlign.Left;
...
for (int i = 1; i <= numAnswers; i++)
{
tmpItem = new ListItem("", i.ToString());
tmpRBL.Items.Add(tmpItem);
}
...
p.Controls.Add(tmpRBL);
...
questionPanel.Controls.Add(p);
}
How can I retrieve the selectedIndexes of these dynamically created RadioButtonLists? I've spent the better part of the day trying various fixes from other similar questions online, but have had no luck.
If I use 'Inspect Element' in Chrome, I am able to see the RadioButtonLists in their desired locations ostensibly with the ID I have assigned (RadioButtonList1, RadioButtonList2, etc), but I everything I try ends up with a null object.
I'm relatively new to C# and this is my first time dealing with dynamic controls, so big thanks in advance for any help offered.
Thank's a lot. In the same way, I, finnaly managed to put other controls under control :-). Here is code-behind in vb.net.
Me now very lucky :-)
Creating & Retrieving values from dynamically created RadioButtonList & TextBox
.aspx
Basically, if you create a control dynamically, you will need to reload those controls (with same id) in every post back of the page.
Otherwise, they will become null, and you won't be able to access them.
Here is a sample. It loads
RadioButtonList
control dynamically and displays the selected value back when a button is clicked.