无法从C#上的gridview上的单选按钮获取值

时间:2013-08-27 14:49:07

标签: c# asp.net gridview

你好我有一个带有4个单选按钮的Gridview,我想从中获取它们的价值,无论我做什么,价值总是假的,有人能说出我的错误在哪里吗?

这是gridview的代码:

 <asp:GridView ID="GridView8" runat="server" Width="903px" 
         Height="516px" CellPadding="4" ForeColor="#333333" GridLines="None" 
         Visible="False"  
         >
                     <AlternatingRowStyle BorderColor="Black" BackColor="White" />

     <Columns>
                         <asp:TemplateField HeaderText="Solicitante/">
                             <ItemTemplate>
                                 <asp:RadioButton ID="optCl1" runat="server" Text="SI" GroupName="optCl" />
                                <asp:RadioButton ID="optCl2" runat="server" Text="NO" GroupName="optCl" />
                             </ItemTemplate>
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="CoGarante">
                             <ItemTemplate >
                             <asp:RadioButton ID="optGar1" runat="server" Text="SI" GroupName="optGar" />
                                <asp:RadioButton ID="optGar2" runat="server" Text="NO" GroupName="optGar" />

                             </ItemTemplate>
                         </asp:TemplateField>
                     </Columns>

                     <EditRowStyle BorderColor="Black" />
                     <FooterStyle BackColor="#990000" BorderColor="Black" ForeColor="White" 
                         Font-Bold="True" />
                     <HeaderStyle BackColor="#990000" BorderColor="Black" Font-Bold="True" 
                         ForeColor="White" />
                     <PagerStyle ForeColor="#333333" HorizontalAlign="Center" BackColor="#FFCC66" />
                     <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                     <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                     <SortedAscendingCellStyle BackColor="#FDF5AC" />
                     <SortedAscendingHeaderStyle BackColor="#4D0000" />
                     <SortedDescendingCellStyle BackColor="#FCF6C0" />
                     <SortedDescendingHeaderStyle BackColor="#820000" />

     </asp:GridView>

读取radiobutton的函数代码

 protected void saveQuestions()
{       
    foreach (GridViewRow row in GridView8.Rows)
    {
        RadioButton rb = row.Cells[2].FindControl("optGar2") as RadioButton;
        Response.Write(rb.Checked);
    }

    conn.Close();
}

在gridview上设置数据的函数代码:

protected void loadQuestions()
{
    OdbcConnection conn = connection();
    conn.Open();

    OdbcCommand findSql = new OdbcCommand("SELECT question AS PREGUNTAS,id FROM questionary_reg WHERE(status='1')", conn);
    GridView8.DataSource = null;

    DataTable dt = new DataTable();
    dt.Load(findSql.ExecuteReader());

    GridView8.DataSource = dt;
    GridView8.DataBind();


    conn.Close();

}

5 个答案:

答案 0 :(得分:3)

问题是因为发生回发并重置gridview中的值,请确保 在if!Postback ONLY上调用此函数loadQuestions()

if(!IsPostBack){
  loadQuestions();
}

#UPDATE 1工作代码:
//设计

<asp:GridView runat="server" ID="gv">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:RadioButton runat="server" ID="rd" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:Button runat="server" ID="btn" onclick="btn_Click" />

//代码

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStringDb1"].ToString()))
        {
            try
            {
                String cmdText = "SELECT * FROM Image WHERE IsDeleted=@isDeleted";
                SqlCommand cmd = new SqlCommand(cmdText, cn);
                cmd.Parameters.AddWithValue("@IsDeleted", "false");
                cn.Open();
                SqlDataAdapter myAdapter = new SqlDataAdapter(cmd);
                DataTable dt_Category = new DataTable();
                myAdapter.Fill(dt_Category);
                cn.Close();

                gv.DataSource = dt_Category;
                gv.DataBind();
            }
            catch (Exception ex)
            {
            }
        }
    }
}
protected void btn_Click(object sender, EventArgs e)
{
    foreach (GridViewRow gvr in gv.Rows)
    {
        RadioButton rd = (RadioButton)gvr.FindControl("rd");
        if (rd.Checked)
        {
        }
        else
        {
        }
    }
}

答案 1 :(得分:1)

也许你需要一个&#39; CheckedChanged&#39;事件:(经过测试和工作)

在ASPX中设置(在本例中,您可以看到标签显示所选行数)

                    <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:RadioButton ID="rbtnSelect" AutoPostBack="true" runat="server" OnCheckedChanged="rbtnSelect_CheckedChanged" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>

和代码存储集

        protected void rbtnSelect_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton selectButton = (RadioButton)sender;
        GridViewRow row = (GridViewRow)selectButton.Parent.Parent;
        int a = row.RowIndex;
        foreach (GridViewRow rw in gvCursos.Rows)
        {
            if (selectButton.Checked)
            {

                if (rw.RowIndex != a)
                {
                    lbResultado.Text = rw.RowIndex.ToString();
                    RadioButton rd = rw.FindControl("rbtnSelect") as RadioButton;
                    rd.Checked = false;
                }
            }
        }
    }

答案 2 :(得分:0)

改变这个:

RadioButton rb = row.Cells[2].FindControl("optGar2") as RadioButton;

对此:

RadioButton rb = row.FindControl("optGar2") as RadioButton;

答案 3 :(得分:0)

for (int i = 0; i < GridView8.Rows.Count; i++)
        {
            if (GridView8.Rows[i].RowType == DataControlRowType.DataRow)
            {
                RadioButton rb= (RadioButton)grdView.Rows[i].FindControl("optGar2");  
                Response.Write(rb.Checked);                   
            }
        }

答案 4 :(得分:-1)

foreach (GridViewRow gvp in gridView1.Rows)
    {
        System.Web.UI.HtmlControls.HtmlInputRadioButton rd = (System.Web.UI.HtmlControls.HtmlInputRadioButton)gvp.FindControl("rd");
        if (rd.Checked)
        {
            string s = rd.Value;
        }
        else
        {
        }
    }

设计视图

<ItemTemplate> <input runat="server" id='rd' type="radio" value='<%# Eval("id") %>' onclick="javascript:SelectSingleRadiobutton(this.id)" /> </ItemTemplate>