ASP.net looping through controls in a table
i have a table which contains a bunch of开发者_开发百科 dynamically created radio button lists, im trying to write code which will loop through each one of the radio button list and get the text value of the selected item. i have the following code
foreach ( Control ctrl in Table1.Controls)
{
if (ctrl is RadioButtonList)
{
//get the text value of the selected radio button
}
}
but i am stuck on how i can get the value of the selected item for that given control.
Try this:
foreach (Control ctrl in Table1.Controls)
{
if (ctrl is RadioButtonList)
{
RadioButtonList rbl = (RadioButtonList)ctrl;
for (int i = 0; i < rbl.Items.Count; i++)
{
if (rbl.Items[i].Selected)
{
//get the text value of the selected radio button
string value = rbl.Items[i].Text;
}
}
}
}
To determine the selected items in the RadioButtonList control, iterate through the Items collection and test the Selected property of each item in the collection.
Look here: RadioButtonList Web Server Control
精彩评论