ASP.net遍历表中的控件

c11*_*ada 1 asp.net radiobuttonlist

我有一个表,其中包含一堆动态创建的单选按钮列表,我试图编写代码,该代码将遍历每个单选按钮列表并获取所选项目的文本值。我有以下代码

   foreach ( Control ctrl in Table1.Controls)
    {
        if (ctrl is RadioButtonList)
        {
           //get the text value of the selected radio button 
        }
    }
Run Code Online (Sandbox Code Playgroud)

但我仍然坚持如何获取给定控件的所选项目的价值。

Len*_*rri 5

尝试这个:

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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

若要确定RadioButtonList控件中的选定项目,请遍历Items集合并测试该集合中每个项目的Selected属性。

在这里查看:RadioButtonList Web服务器控件