.net中点击全选按钮实现GridView中复选框全选
- 提问者网友:人生佛魔见
- 2021-07-30 07:04
- 五星知识达人网友:轮獄道
- 2021-07-30 07:45
双击button事件写如下代码就可以了
for (int i=0; i < this.GridView1.Rows.Count; i++)
{
CheckBox chk =(CheckBox) this.GridView1.Rows[i].FindControl("CheckBox1");
chk .Checked =true ;
}
- 1楼网友:拾荒鲤
- 2021-07-30 09:10
用一段JavaScript就能搞定
<script type="text/javascript"> function selectAll(chk) { var items=document.getElementsByTagName("input"); for(var i=0;i<items.length;i++) { if(items[i].type=="checkbox") { items[i].checked=chk.checked; } } }
</script>
在那代码段加事件
<Columns> <asp:TemplateField> <HeaderTemplate> <asp:Button ID="Button3" runat="server" onclick="selectAll(this)" Text=" 全选" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" /> <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" /> <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" /> <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" /> </Columns>