有一Table,动态生成,若干行和列。
Table里面有Label,TextBox,RadioButtonList等控件。
要取出所有RadioButtonList的值,如何做?自已写的:
foreach(Control c in Table1.Controls)
{
Response.Write(c.ToString()+"<br>");
if(c.ToString()=="System.Web.UI.WebControls.RadioButtonList")
{
RadioButtonList r=new RadioButtonList();
r=(RadioButtonList)c;
if(r.SelectedIndex!=-1)
{
Response.Write("Text="+r.SelectedItem.Text+"<br>");
Response.Write("value="+r.SelectedItem.Value+"<br>");
}
}
}通过Response.Write(c.ToString()+"<br>");测试输出
发现取出来的值是System.Web.UI.WebControls.TableRow,并不是RadioButtonList,请问?

解决方案 »

  1.   

    foreach (ListItem item in RadioButtonList.Item)
    {
    Response.Write(item.value);
    }
      

  2.   

    you should doforeach(TableRow tr in Table1.Rows)
    {
       RadioButtonList r = (RadioButtonList)tr.FindControl("YourControldID");
       if(r.SelectedIndex!=-1)
       {
          Response.Write("Text="+r.SelectedItem.Text+"<br>");
          Response.Write("value="+r.SelectedItem.Value+"<br>");
       }
    }
    otherwise
    foreach(TableRow tr in Table1.Rows)
    {
      foreach (TableCell tc in tr.Cells)
      {
    foreach(Control c in tc.Controls)
    {
    if(c is RadioButtonList)
    {
    RadioButtonList r=(RadioButtonList)c;
    if(r.SelectedIndex!=-1)
    {
    Response.Write("Text="+r.SelectedItem.Text+"<br>");
    Response.Write("value="+r.SelectedItem.Value+"<br>");
    }
    }
    }  }}
      

  3.   

    支持如下代码:
    foreach(TableRow tr in Table1.Rows)
    {
       RadioButtonList r = (RadioButtonList)tr.FindControl("YourControldID");
       if(r.SelectedIndex!=-1)
       {
          Response.Write("Text="+r.SelectedItem.Text+"<br>");
          Response.Write("value="+r.SelectedItem.Value+"<br>");
       }
    }
    otherwise
    foreach(TableRow tr in Table1.Rows)
    {
      foreach (TableCell tc in tr.Cells)
      {
    foreach(Control c in tc.Controls)
    {
    if(c is RadioButtonList)
    {
    RadioButtonList r=(RadioButtonList)c;
    if(r.SelectedIndex!=-1)
    {
    Response.Write("Text="+r.SelectedItem.Text+"<br>");
    Response.Write("value="+r.SelectedItem.Value+"<br>");
    }
    }
    }  }}