//这个是一个按钮控件的click事件代码
protected void BtnSumit_Click(object sender, EventArgs e)
        {
            
                foreach (GridViewRow row in Gridview1.Rows)
                {
                    
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        
                        string Grade = ((RadioButtonList)row.FindControl("RadList1")).SelectedValue;                    }
                }
        }在我的gridview中自定义了radioButtonList控件:
                                            <ItemTemplate>
                                                <asp:RadioButtonList ID="RadList1" runat="server" 
                                                    RepeatDirection="Horizontal" AppendDataBoundItems="True">
                                                    <asp:ListItem Value="A">A</asp:ListItem>
                                                    <asp:ListItem Value="B">B</asp:ListItem>
                                                    <asp:ListItem Value="C">C</asp:ListItem>
                                                    <asp:ListItem Value="D">D</asp:ListItem>
                                                </asp:RadioButtonList>
                                            </ItemTemplate>
出现的问题:在网页Gridview1中点击RadioButtonList控件,然后点击提交按钮,响应按钮事件时,使用FindControl是可以找得到RadioButtonList控件的,但是string Grade = ((RadioButtonList)row.FindControl("RadList1")).SelectedValue;
这句代码Grade的值却为空。求各路大侠指点!!!!!!

解决方案 »

  1.   

    为什么要这样写呢?this.RadList1.selectedItem[0]这样不行吗?
      

  2.   

    我要的是获取我在RadList1上选择的内容,这样只是获取了RadList1的第一项
      

  3.   


    写了个 你改成这样看看protected void BtnSumit_Click(object sender, EventArgs e)
      {
     foreach (GridViewRow row in Gridview1.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        RadioButtonList rbs = ((RadioButtonList)row.FindControl("RadList1"));
                        for (int i = 0; i < rbs.Items.Count; i++)
                        {
                            if (rbs.Items[i].Selected == true)
                            { 
                           Response.Write(rbs.Items[i].Text)//or Value
                            }
                        }
                    }
                }
      }
      

  4.   

     protected void Button1_Click(object sender, EventArgs e)
            {
                string selectValue = string.Empty;
                foreach (GridViewRow item in this.gvList.Rows)
                {
                    RadioButtonList rList = (RadioButtonList)item.FindControl("RadList1");
                    if (rList.SelectedValue != string.Empty)
                    {
                        selectValue += rList.SelectedValue + ";";
                    }
                }
                //输出每个RadioButtonList选择的值
                Response.Write(selectValue);
            }
      

  5.   

    问题解决了,是页面回发的问题。在页面回发时在Page_Load事件中Gridview又一次绑定数据,所以RadioButtonList会被重置,值就相当于没有了。谢谢大家了!
      

  6.   

     protected void Button1_Click(object sender, EventArgs e)
            {
                foreach (GridViewRow row in this.GridView1.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        RadioButtonList button = ((RadioButtonList)row.FindControl("RadioButtonList"));
                        ListItem item = button.SelectedItem;
                        string value = item.Value;
                        Response.Write("<script>alert('" + value + "');</script>");
                    }
                }
            }