Select All Checkboxes In GridView Using jQuery In ASP.NET C#

Sometimes simple things go wrong, just like this simple functionality of "Select all" took my whole day. This is also important in the clumsy world, no one has that much time to check the boxes one by one. It is okay if there are 4-5 checkboxes but what about when it comes to 100 – 200 – that's a bit time consuming, so to shorthand that here, in this article I will show you how to select and deselect all the checkboxes in GridView using jQuery in ASP.NET C#.

Initial Chamber

Step 1: Open your Visual Studio 2010 and create an Empty Website, Give a suitable name, gridview_demo.

Step 2: In Solution Explorer, you get your empty website, Add a web form, SQL Database. Here are the steps,

For Web Form:

gridview_demo (Your Empty Website) - Right Click, Add New Item, then Web Form. Name it gridview_demo.aspx.

For SQL Server Database:

gridview_demo (Your Empty Website) - Right Click, Add New Item, then SQL Server Database. Add Database inside the App_Data_folder.

Database Chamber:

Step 3: 

Get to your Database [Database.mdf], we will create a table -  tbl_Data, Go to the database.mdf - Table, then add New table, design your table like the following:

Table - tbl_data [Don’t forget to make ID - Identity Specification, click Yes]

table

Design Chamber

Step 5: 

Now, open your gridview_demo.aspx file, were we create our design by taking Gridview from the toolbox, After dragging the control – add 4 bound field button – Name it as : ID, Name , Location and Select All. You have to change the “Select All field” to – “Template Field”.

Template Field:

We use Template Fields when we wish to display ASP.NET controls in a GridView column. We display ASP.NET controls in a GridView column to provide additional functionality in the user interface.

Here are some beautiful answers regarding Template Fields –
Template Fields.

Gridview_demo.aspx:

  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>  
  2.     <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3.     <html xmlns="http://www.w3.org/1999/xhtml">  
  4.         <head runat="server">  
  5.             <title></title>  
  6.             <script type="text/javascript" language="javascript"> functionCheckall(Checkbox) { var GridView1 = document.getElementById("  
  7.                 <%=GridView1.ClientID %>"); for (i = 1; i  
  8.                     < GridView1.rows.length; i++) { GridView1.rows[i].cells[3].getElementsByTagName( "INPUT")[0].checked=C heckbox.checked; } } </script>  
  9.                         </head>  
  10.   
  11.                         <body>  
  12.                             <form id="form1" runat="server">  
  13.                                 <div>  
  14.                                     <asp:GridViewID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">  
  15.                                         <AlternatingRowStyleBackColor="White" />  
  16.                                         <Columns>  
  17.                                             <asp:TemplateField>  
  18.                                                 <HeaderTemplate>  
  19.                                                     <asp:CheckBoxID="CheckBox1" AutoPostBack="true" OnCheckedChanged="chckchanged" runat="server" /> </HeaderTemplate>  
  20.                                                 <ItemTemplate>  
  21.                                                     <asp:CheckBoxID="CheckBox2" runat="server" /> </ItemTemplate>  
  22.                                             </asp:TemplateField>  
  23.                                             <asp:BoundFieldDataField="id" HeaderText="ID" />  
  24.                                             <asp:BoundFieldDataField="name" HeaderText="Name" />  
  25.                                             <asp:BoundFieldDataField="location" HeaderText="Location" /> </Columns>  
  26.                                         <FooterStyleBackColor="#990000" Font-Bold="True" ForeColor="White" />  
  27.                                         <HeaderStyleBackColor="#990000" Font-Bold="True" ForeColor="White" />  
  28.                                         <PagerStyleBackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />  
  29.                                         <RowStyleBackColor="#FFFBD6" ForeColor="#333333" />  
  30.                                         <SelectedRowStyleBackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />  
  31.                                         <SortedAscendingCellStyleBackColor="#FDF5AC" />  
  32.                                         <SortedAscendingHeaderStyleBackColor="#4D0000" />  
  33.                                         <SortedDescendingCellStyleBackColor="#FCF6C0" />  
  34.                                         <SortedDescendingHeaderStyleBackColor="#820000" /> </asp:GridView>  
  35.                                 </div>  
  36.                                 </form>  
  37.                         </body>  
  38.   
  39.                         </html>  


table
Code Chamber:

Step 6: 

Open your gridview_demo.aspx.cs, here in this part we will bind our GridView by the database so that whatever the data is there in the database gets displayed here and after that we will write our code for checkboxes in the event of Checkbox OnCheckedChanged.

OnCheckedChanged :The CheckedChanged event is raised when the value of the Checked property changes between posts to the server.

Gridview_demo.cs:

  1. using System;  
  2.   
  3. using System.Collections.Generic;  
  4.   
  5. using System.Linq;  
  6.   
  7. using System.Web;  
  8.   
  9. using System.Web.UI;  
  10.   
  11. using System.Web.UI.WebControls;  
  12.   
  13. using System.Data;  
  14.   
  15. using System.Data.SqlClient;  
  16.   
  17. public partial class_Default: System.Web.UI.Page  
  18.   
  19. {  
  20.   
  21.     protected void Page_Load(object sender, EventArgs e)  
  22.   
  23.     {  
  24.   
  25.         if (!IsPostBack)  
  26.   
  27.         {  
  28.   
  29.             refreshdata();  
  30.   
  31.         }  
  32.   
  33.     }  
  34.   
  35.     public void refreshdata()  
  36.   
  37.     {  
  38.   
  39.         SqlConnection con = newSqlConnection(@ "Data Source=Nilesh;InitialCatalog=nil_db;Integrated Security=True");  
  40.   
  41.         con.Open();  
  42.   
  43.         SqlCommandcmd = newSqlCommand("select * from tbl_data", con);  
  44.   
  45.         SqlDataAdaptersda = newSqlDataAdapter(cmd);  
  46.   
  47.         DataTable dt = newDataTable();  
  48.   
  49.         sda.Fill(dt);  
  50.   
  51.         GridView1.DataSource = dt;  
  52.   
  53.         GridView1.DataBind();  
  54.   
  55.     }  
  56.   
  57.     protected void chckchanged(object sender, EventArgs e)  
  58.   
  59.     {  
  60.   
  61.         CheckBox chckheader = (CheckBox) GridView1.HeaderRow.FindControl("CheckBox1");  
  62.   
  63.         foreach(GridViewRow row in GridView1.Rows)  
  64.   
  65.         {  
  66.   
  67.             CheckBoxchckrw = (CheckBox) row.FindControl("CheckBox2");  
  68.   
  69.             if (chckheader.Checked == true)  
  70.   
  71.             {
  72.                 chckrw.Checked = true;
  73.             } else  
  74.   
  75.             {
  76.                 chckrw.Checked = false;
  77.             }  
  78.   
  79.         }  
  80.   
  81.     }  
  82.   
  83. }  

Output Chamber:

output

Hope you like it. Thank you for reading.

Read more articles on ASP.NET:


Similar Articles