第一种方法
-------前端 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();
- }
- }
复制代码
希望能帮到大家~~~~~~~~ 来源:https://blog.csdn.net/weixin_42864782/article/details/83650395 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |