gridview checkbox implements full selection and unselection from both the server side and the client side

  • 2020-05-24 05:20:53
  • OfStack

In GridView, full selection and unselection of checkbox are required to be implemented in many places, so the selection of checkbox is realized from both the server side and the client side.
1. Server side:
The html code is as follows:
 
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
DataKeyNames="ID" DataSourceID="SqlDataSource1"> 
<Columns> 
<asp:TemplateField> 
<HeaderTemplate> 
<asp:CheckBox ID="CheckAll" runat="server" OnCheckedChanged="CheckAll_CheckedChanged" AutoPostBack="true" /> 
</HeaderTemplate> 
<ItemTemplate> 
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" /> 
</ItemTemplate> 
</asp:TemplateField> 
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
ReadOnly="True" SortExpression="ID" /> 
<asp:BoundField DataField="num" HeaderText="num" SortExpression="num" /> 
</Columns> 
</asp:GridView> 
 The acquisition of data is not the focus, so I choose to use sqldatasource Control to achieve the acquisition of data.  GridView The use of the BoundField The binding ID,num The fields of the two data tables. In the above HTML In the code, template columns are used  
<asp:TemplateField> 
<HeaderTemplate> 
<asp:CheckBox ID="CheckAll" runat="server" OnCheckedChanged="CheckAll_CheckedChanged" AutoPostBack="true" /> 
</HeaderTemplate> 
<ItemTemplate> 
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" /> 
</ItemTemplate> 
</asp:TemplateField> 

The header section is the fully selected checkbox section, and the item section is the single checkbox section. (please note that AutoPostBack should be set to true, otherwise the server-side code cannot be touched)
Specific background code:
 
protected void CheckAll_CheckedChanged(object sender,EventArgs e) 
{ 
CheckBox ck = sender as CheckBox; 
if (ck != null) 
{ 
System.Web.UI.WebControls.GridView g = ck.NamingContainer.NamingContainer as System.Web.UI.WebControls.GridView; 
for (Int32 i = 0; i < g.Rows.Count; i++) 
{ 
(g.Rows[i].FindControl("CheckBox1") as CheckBox).Checked = ck.Checked; 
} 
} 
} 
protected void CheckBox1_CheckedChanged(object sender,EventArgs e) 
{ 
var count = 0; 
CheckBox ck = sender as CheckBox; 
if (ck != null) 
{ 
System.Web.UI.WebControls.GridView g = ck.NamingContainer.NamingContainer as System.Web.UI.WebControls.GridView; 
for (Int32 i = 0; i < g.Rows.Count; i++) 
{ 
if ((g.Rows[i].FindControl("CheckBox1") as CheckBox).Checked) 
{ 
count++; 
} 
} 
(g.HeaderRow.FindControl("CheckAll") as CheckBox).Checked =count==g.Rows.Count; 
} 
} 

After running the page, you can see that you can select all of checkbox by clicking on all of them. Cancel all checkbox, then all checkbox also cancel. If all one of the single checkbox is selected, then all of the selected checkbox is also selected. If a single checkbox is not selected, then the all-selected checkbox is not selected either.
Here is the client implementation:
For the html code section, please remove the OnCheckedChanged and AutoPostBack of the two checkbox. Everything else is the same.
 
<script type="text/javascript"> 
$(function() { 
$("#GridView1 :checkbox").eq(0).click(function() { 
if ($(this).is(":checked")) { 
$(this).parent().parent().nextAll().find(":checkbox").attr("checked", "checked"); 
} 
else { 
$(this).parent().parent().nextAll().find(":checkbox").removeAttr("checked"); 
} 
}); 
$("#GridView1 :checkbox").not($("#GridView1 :checkbox:first")).click(function() { 
if ($(this).is(":checked")) { 
if ($("#GridView1 :checked").length == $("#GridView1 :checkbox").length - 1) { 
$("#GridView1 :checkbox").eq(0).attr("checked", "checked"); 
} 
} 
else { 
$("#GridView1 :checkbox").eq(0).removeAttr("checked"); 
} 
}); 
}); 
</script> 

Related articles: