1

您好,我有一个带有 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>

读取单选按钮的函数代码

 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();

}
4

5 回答 5

3

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

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
        {
        }
    }
}
于 2013-08-27T15:34:34.127 回答
1

也许你需要一个 'CheckedChanged' 事件:(测试和工作)

在 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;
                }
            }
        }
    }
于 2016-09-10T00:06:58.983 回答
0

改变这个:

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

对此:

RadioButton rb = row.FindControl("optGar2") as RadioButton;
于 2013-08-27T14:50:22.410 回答
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);                   
            }
        }
于 2013-08-27T15:35:14.203 回答
-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>

于 2013-12-14T05:41:37.513 回答