1

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

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

但我坚持如何获得该给定控件的选定项目的值。

4

1 回答 1

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;
            }
        }
    }
}

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

看这里:RadioButtonList Web 服务器控件

于 2010-03-21T19:48:25.050 回答