ASP.net looping through controls in a table

时光毁灭记忆、已成空白 提交于 2020-01-25 04:57:10

问题


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:


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



来源:https://stackoverflow.com/questions/2488462/asp-net-looping-through-controls-in-a-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!