Search This Blog

Sunday, April 8, 2012

Check Uncheck all CheckBoxes in an ASP.NET GridView using jQuery


how to add a checkbox to the header so that i select/deselect all checkboxes. I do not want postbacks to occur when I perform this action.”

ASPX Page (with jQuery script)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs"Inherits="Default4" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Check Uncheck All Checkboxes In A GridView</title>
<script type='text/javascript'src='http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js'></script>
<script type="text/javascript">
    $(document).ready(function() {
        var chkBox = $("input[id$='ChkAll']");
        chkBox.click(
             function() {
                 $("#GridView1 INPUT[type='checkbox']")
                 .attr('checked', chkBox
                 .is(':checked'));
             });

        // To deselect CheckAll when a GridView CheckBox
        // is unchecked
        $("#GridView1 INPUT[type='checkbox']").click(
        function(e) {
            if (!$(this)[0].checked) {
                chkBox.attr("checked", false);
            }
        });
    });
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
    DataKeyNames="ID" >
    <Columns>  
        <asp:TemplateField>
            <HeaderTemplate>
                <asp:CheckBox ID="ChkAll" runat="server" />
            </HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="chkSel" class="chkSel" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="ID" HeaderText="EmployeeId"
            SortExpression="EmployeeId" />
        <asp:BoundField DataField="FName" HeaderText="FirstName"
            SortExpression="FirstName" />
         <asp:BoundField DataField="Age" HeaderText="Age"
            SortExpression="Age" />
        <asp:BoundField DataField="Sex" HeaderText="Sex"
            SortExpression="Sex" />
    </Columns>
    </asp:GridView>
    <br />
   
    <asp:Button ID="btnRetrieveCheck" runat="server"
    Text="Retrieve Checked Items" onclick="btnRetrieveCheck_Click" />
</div>
</form>
</body>
</html>



The jQuery code shown above finds all elements of the checkbox type inside the GridView and sets the ‘checked’ value of all items in it to true or false based on the checked value of the ‘ChkAll’ checkbox. The script also takes care of unchecking the ‘ChkAll’ checkbox, when one of the checkboxes in the GridView is unchecked. This part of the script was shared by Tim Hobbs.
C#
using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default4 : System.Web.UI.Page{
    List<Employee> listEmp = new List<Employee>();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {         
            Employee emp = new Employee();
            listEmp = emp.GetEmployees();
            this.GridView1.DataSource = listEmp;
            this.GridView1.DataBind();
        }
    }

    protected void btnRetrieveCheck_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)row.FindControl("chkSel");
            if (cb != null && cb.Checked)
            {
                Response.Write("Employee Id: " +
                    GridView1.DataKeys[row.RowIndex].Value
                    + " Name: " + row.Cells[2].Text + "<br/>");
            }
        }
    }
}

public class Employee{

public int ID { get; set; }

public string FName { get; set; }

public int Age { get; set; }

public char Sex { get; set; }

public List<Employee> GetEmployees()
{
List<Employee> eList = new List<Employee>();

eList.Add(new Employee() { ID = 1, FName = "John", Age = 23, Sex = 'M' });

eList.Add(new Employee() { ID = 2, FName = "Mary", Age = 25, Sex = 'F' });

eList.Add(new Employee() { ID = 3, FName = "Amber", Age = 23, Sex = 'M' });

eList.Add(new Employee() { ID = 4, FName = "Kathy", Age = 25, Sex = 'M' });

eList.Add(new Employee() { ID = 5, FName = "Lena", Age = 27, Sex = 'F' });

eList.Add(new Employee() { ID = 6, FName = "John", Age = 28, Sex = 'M' });

eList.Add(new Employee() { ID = 7, FName = "Kathy", Age = 27, Sex = 'F' });

eList.Add(new Employee() { ID = 8, FName = "John", Age = 28, Sex = 'M' });

return eList;
}
 

}

No comments:

Post a Comment