加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 08:19:57 所属栏目:百科 来源:网络整理
导读:我在gridview中使用了复选框….我在第一个单元格中使用它….当我在运行时选择复选框时,我需要获取这些值…但是在选择或单击复选框时,它不是发现或价值是假的…如何在asp.net后端和c#代码中写? asp:TemplateField ItemTemplate asp:checkbox id="ShowAddress
我在gridview中使用了复选框….我在第一个单元格中使用它….当我在运行时选择复选框时,我需要获取这些值…但是在选择或单击复选框时,它不是发现或价值是假的…如何在asp.net后端和c#代码中写?
<asp:TemplateField>
   <ItemTemplate >
       <asp:checkbox id="ShowAddress" runat="server" />
  </ItemTemplate>
 </asp:TemplateField>

代码隐藏:

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
            }
        }
    }

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

有人可以帮忙吗?

解决方法

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

你的chkBx变量是null吗?

以下代码有效:

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)
            {
                // ...
            }
        }
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读