Gridview Checkbox Check And Uncheck Using jQuery In ASP.NET

Introduction

This article gives an introduction of how to select or deselect single and multiple GridView rows using jQuery.

Background

You may have seen this type of check/uncheck checkbox control in email (like Gmail, Hotmail, Yahoo etc.) GridView. If you are using a checkbox control in GridView, then necessarily add the header checkbox in a GridView header.

Requirement

ASP.NET GridView and jQuery library.

We will create a GridView with checkbox step by step.

Step 1 Start Visual Studio.

Step 2 Add Web Application.

Step 3 Select Solution Explorer.

Right click on project and got to Add - Add New Item.



Add new Web form with the name Gridview.aspx



Step 4

Go to Gridview.aspx page >> Toolbox >> Add GridView.



Drag and drop the default GridView, as shown below.



In default GridView, we bind two data fields from our database table and create checkbox control using GridView TemplateField.

Modified GridView is shown below.



The design code of GridView bound column TemplateField is shown below.



Now, we will bind the data from data table.
  • Connect Web application with the database, as given below. Go to Menu - Tools - Connect to Database



  • Define database connection string in project web.config file.



  • Access the database connection string from web.config file on Gridview.aspx.cs, as shown below.



  • The GridView is bound with the database using SqlDataAdapter of Gridview.aspx.cs.


Now, we need to write a few lines of jQuery code to the checked or unchecked function for GridView header checkbox and GridView row checkbox.

Add the jQuery reference file on gridview.aspx page, as shown below.



Now, write the jQuery function code for GridView checkbox on gridview.aspx page.



The full code of Gridview.aspx is given below.

  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Master/Master.master" AutoEventWireup="true" CodeFile="Gridview.aspx.cs" Inherits="Jquery_Example_Gridview" %>  
  2.     <aspContent ID="Content1" ContentPlaceHolderID="head" runat="Server">  
  3.         <script type="text/javascript" src="http//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>  
  4.         <script type="text/javascript">  
  5.             $(document).ready(function() {  
  6.                 //Checked or unchecked All Checkboxes inside grid with header checkbox   
  7.                 $("#<%= GridEmpRecord.ClientID %> input[id*='chkHeader']").click(function() {  
  8.                     if ($(this).is('checked'))  
  9.                         //Checkd All Child checkbox  
  10.                         $("#<%= GridEmpRecord.ClientID %> input[id*='chkChild']").attr('checked'this.checked);  
  11.                     else  
  12.                         //UnCheckd All Child checkbox  
  13.                         $("#<%= GridEmpRecord.ClientID %> input[id*='chkChild']").removeAttr('checked'this.checked);  
  14.                 });  
  15.                 // Header checkbox checked or unchecked inside grid row checkboxes   
  16.                 $("#<%=GridEmpRecord.ClientID%> input[id*='chkChild']checkbox").click(function() {  
  17.                     //Get All Checkbox inside grid  
  18.                     var GetAllCheckboxes = $("#<%=GridEmpRecord.ClientID%> input[id*='chkChild']checkbox").size();  
  19.                     //Get number of checked checkboxes inside grid   
  20.                     var MarkcheckedCheckboxes = $("#<%=GridEmpRecord.ClientID%> input[id*='chkChild']checkboxchecked").size();  
  21.                     //Check / Uncheck top checkbox if all the checked boxes in list are checked  
  22.                     $("#<%=GridEmpRecord.ClientID%> input[id*='chkHeader']checkbox").attr('checked', GetAllCheckboxes == MarkcheckedCheckboxes);  
  23.                 });  
  24.             });  
  25.         </script>  
  26.     </aspContent>  
  27.     <aspContent ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">  
  28.         <aspGridView runat="server" ID="GridEmpRecord" AutoGenerateColumns="False" Height="262px" Width="301px">  
  29.             <Columns>  
  30.                 <aspTemplateField>  
  31.                     <HeaderTemplate>  
  32.                         <aspCheckBox ID="chkHeader" runat="server" /> </HeaderTemplate>  
  33.                     <ItemTemplate>  
  34.                         <aspCheckBox ID="chkChild" runat="server" /> </ItemTemplate>  
  35.                 </aspTemplateField>  
  36.                 <aspTemplateField HeaderText="Name">  
  37.                     <ItemTemplate>  
  38.                         <%#Eval("EmpName")%>  
  39.                     </ItemTemplate>  
  40.                 </aspTemplateField>  
  41.                 <aspTemplateField HeaderText="Department">  
  42.                     <ItemTemplate>  
  43.                         <%#Eval("EmpDepartment")%>  
  44.                     </ItemTemplate>  
  45.                 </aspTemplateField>  
  46.             </Columns>  
  47.         </aspGridView>  
  48.     </aspContent>  
The full code of gridview.aspx.cs given as below,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. using System.Configuration;  
  10. public partial class Jquery_Example_Gridview System.Web.UI.Page {  
  11.     string conString = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;  
  12.     protected void Page_Load(object sender, EventArgs e) {  
  13.         EmployeeDataGrid();  
  14.     }  
  15.     private void EmployeeDataGrid() {  
  16.         using(SqlConnection con = new SqlConnection(conString)) {  
  17.             using(SqlCommand cmd = new SqlCommand("select EmpName,EmpDepartment from EmployeeDB")) {  
  18.                 SqlDataAdapter dt = new SqlDataAdapter();  
  19.                 try {  
  20.                     cmd.Connection = con;  
  21.                     con.Open();  
  22.                     dt.SelectCommand = cmd;  
  23.                     DataTable dTable = new DataTable();  
  24.                     dt.Fill(dTable);  
  25.                     GridEmpRecord.DataSource = dTable;  
  26.                     GridEmpRecord.DataBind();  
  27.                 } catch (Exception) {  
  28.                     // error code   
  29.                 }  
  30.             }  
  31.         }  
  32.     }  
  33. }  
Now, build the web application page and execute the code.

The output is shown below.