ASP.net looping through controls in a table

2019-07-29 09:27发布

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.

1条回答
爷的心禁止访问
2楼-- · 2019-07-29 10:06

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

查看更多
登录 后发表回答