asp.net中,GridView绑定的选择框CheckBox,多选后获取选中的id

第一种方法

-------前端
GridViewRow 中需要添加属性:DataKeyNames=“user_id” //user_id 为要获取的id

<asp:GridView ID="GridView1" runat="server" DataKeyNames="user_id ">
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:CheckBox runat="server"  ID="CheckBoxId">
                                        </asp:CheckBox>
                                    </ItemTemplate>
                                </asp:TemplateField>
  </asp:GridView>

-------后端

        string strTempValue = string.Empty;
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)gvr.FindControl("CheckBoxId");  //CheckBoxId为选择框的id
            if (cb.Checked)
            {
                strTempValue = strTempValue + "," + GridView1.DataKeys[gvr.RowIndex].Value.ToString();
            }
        }

第二种方法

-------前端

<asp:GridView ID="GridView1" runat="server">
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:CheckBox runat="server"  ID="CheckBoxId">
                                        </asp:CheckBox>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                 <asp:BoundField DataField="UserId" HeaderText="ID">
                                </asp:BoundField>
  </asp:GridView>
  
        string strTempValue = string.Empty;
        foreach (GridViewRow g in GridView1.Rows)
        {
            CheckBox ck = (CheckBox)g.FindControl("CheckBoxId");
            if (ck.Checked == true)
            {
                //这里的Cells[1]是因为 在前端UserId列在下标为1的位置
                strTempValue   =strTempValue   + "," + g.Cells[1].Text.ToString(); 
            }
        }

希望能帮到大家~~~~~~~~

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值