如何在使用asp .net检查的gridview中获取CheckBoxes的值

6 c#

我在gridview中使用了复选框....我在第一个单元格中使用它....当我在运行时选择复选框时,我需要获取这些值...但是在选择或单击复选框时,它不是发现或价值是假的...如何在asp.net后端和c#代码中写?

 <asp:TemplateField>
   <ItemTemplate >
       <asp:checkbox id="ShowAddress" runat="server" />
  </ItemTemplate>
 </asp:TemplateField>
Run Code Online (Sandbox Code Playgroud)

代码隐藏:

  protected void Button1_Click(object sender, EventArgs e)
    {
        // Looping through all the rows in the GridView

        foreach (GridViewRow di in GridView1.Rows)
        {
         CheckBox chkBx = (CheckBox)di.FindControl("ShowAddress");

            if (chkBx != null && chkBx.Checked)
            {
                /// put your code here
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

页面加载时脚本中是否有任何实现?

有人可以帮忙吗?

Jak*_*sen 4

如何填充 GridView?如果您在 Page_Load 中执行此操作,请确保您没有在回发中执行此操作(检查 IsPostBack)。

您的 chkBx 变量是否为空?

以下代码有效:

    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox chk = row.Cells[0].Controls[0] as CheckBox;
            if (chk != null && chk.Checked)
            {
                // ...
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)