2010-03-21 51 views
1

我有一个表,其中包含一堆动态创建的单选按钮列表,即时试图编写代码,将通过每个单选按钮列表循环,并获得文本的值选定的项目。我有以下代码ASP.net通过控制在表中循环

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

但我坚持就如何我能得到所选项目的价值为给定的控制。

回答

4

试试这个:

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控件中选定的项目,通过项目集合迭代,并收集在测试每个项目的Selected属性。

看这里:RadioButtonList Web Server Control

+0

\t错误1 \t“System.Web.UI.Control”不包含关于“相关”和没有扩展方法“项”接受型的第一参数“的定义的System.Web .UI.Control“可以找到(您是否缺少使用指令或程序集引用?)\t C:\ Users \ C!\ Documents \ My Dropbox \ Final Year \ Project \ ASP \ WebSite2 \ Default5.aspx.cs C:\ ... \ WebSite2 \ – c11ada 2010-03-21 20:02:56

+0

cast ctrl to radionbuttonlist in the loop'for(int i = 0; i <((RadioButtonList)ctrl).Items.Count; i ++)' – fearofawhackplanet 2010-03-21 20:07:58

+0

@ c11ada:I忘了将crtl投射到RadioButtonList。当我纠正代码fearofawhackplanet写了上面的评论。心灵感应。 :) – 2010-03-21 20:10:37