The gridview checkbox selects both the server side and the client side.

Source: Internet
Author: User

Full and inverse selection of the checkbox in the GridView are required in many places. Therefore, the checkbox selection is implemented from the server side and the client side.
1. Server Side:
The html code is as follows: Copy codeThe 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 = "checkbox#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 data acquisition is not the focus, so we chose to use the sqldatasource Control for data acquisition. In the GridView, BoundField is used to bind the ID and num fields of the two data tables. In the preceding HTML code, the template column is 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 = "checkbox#checkedchanged"/>
</ItemTemplate>
</Asp: TemplateField>

The header part is the fully selected checkbox, and the item part is the single checkbox part. (Please note that AutoPostBack must be set to true, otherwise the server code cannot be touched)
Specific Background code:Copy codeThe Code is as follows: 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 checkbox#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 all selected checkboxes and select all. If all the selected checkboxes are canceled, the checkbox is also deselected. If one checkbox is selected, all checkboxes are also selected. If a single checkbox is not selected, all the checkboxes are not selected.
The implementation of the client is as follows::
In the html code section, remove the OnCheckedChanged and AutoPostBack of the two checkboxes. Others remain unchanged.Copy codeThe Code is as follows: <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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.