Saturday, January 11, 2014

Gridview header checkbox select and deselect all rows using client side JavaScript and server side C#

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Controls_Default" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <script type="text/javascript">
        // Select/Deselect checkboxes based on header checkbox
        function SelectheaderCheckboxes(headerchk) {
            debugger
            var gvcheck = document.getElementById('GridVwRowColorchange');
            var i;
            //Condition to check header checkbox selected or not if that is true checked all checkboxes
            if (headerchk.checked) {
                for (i = 0; i < gvcheck.rows.length; i++) {
                    var inputs = gvcheck.rows[i].getElementsByTagName('input');
                    inputs[0].checked = true;
                }
            }
            //if condition fails uncheck all checkboxes in gridview
            else {
                for (i = 0; i < gvcheck.rows.length; i++) {
                    var inputs = gvcheck.rows[i].getElementsByTagName('input');
                    inputs[0].checked = false;
                }
            }
        }
        //function to check header checkbox based on child checkboxes condition
        function Selectchildcheckboxes(header) {
            var ck = header;
            var count = 0;
            var gvcheck = document.getElementById('GridVwRowColorchange');
            var headerchk = document.getElementById(header);
            var rowcount = gvcheck.rows.length;
            //By using this for loop we will count how many checkboxes has checked
            for (i = 1; i < gvcheck.rows.length; i++) {
                var inputs = gvcheck.rows[i].getElementsByTagName('input');
                if (inputs[0].checked) {
                    count++;
                }
            }
            //Condition to check all the checkboxes selected or not
            if (count == rowcount - 1) {
                headerchk.checked = true;
            }
            else {
                headerchk.checked = false;
            }
        }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="GridVwRowColorchange" runat="server" AutoGenerateColumns="False"
        Font-Names="Verdana" PageSize="5" Width="75%" BorderColor="#CCCCCC" BorderStyle="Solid"
        BorderWidth="1px" OnRowDataBound="GridVwRowColorchange_RowDataBound">
        <AlternatingRowStyle BackColor="#BFE4FF" />
        <PagerStyle BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
        <HeaderStyle Height="30px" BackColor="#6DC2FF" Font-Size="15px" BorderColor="#CCCCCC"
            BorderStyle="Solid" BorderWidth="1px" />
        <RowStyle Height="20px" Font-Size="13px" BorderColor="#CCCCCC" BorderStyle="Solid"
            BorderWidth="1px" />
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox ID="chkheader" runat="server" onclick="javascript:SelectheaderCheckboxes(this)" />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="chkchild" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Emp_Name" HeaderText="Employee Name" />
            <asp:BoundField DataField="Emp_id" HeaderText="Employee ID" />
            <asp:BoundField DataField="Emp_job" HeaderText="Job title" />
            <asp:BoundField DataField="Emp_Dep" HeaderText="Department" />
        </Columns>
    </asp:GridView>
    </form>
</body>
</html>


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
public partial class Controls_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    private void LoadData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Emp_Name");
        dt.Columns.Add("Emp_id");
        dt.Columns.Add("Emp_job");
        dt.Columns.Add("Emp_Dep");
        DataRow rw1 = dt.NewRow();
        rw1["Emp_Name"] = "Narain Sidharth";
        rw1["Emp_id"] = "2020";
        rw1["Emp_job"] = "SOftware Engineer";
        rw1["Emp_Dep"] = "IT";
        dt.Rows.Add(rw1);
        DataRow rw2 = dt.NewRow();
        rw2["Emp_Name"] = "Prakalathan";
        rw2["Emp_id"] = "1978";
        rw2["Emp_job"] = "System Engineer";
        rw2["Emp_Dep"] = "IT";
        dt.Rows.Add(rw2);
        DataRow rw3 = dt.NewRow();
        rw3["Emp_Name"] = "Mathu kumar";
        rw3["Emp_id"] = "2700";
        rw3["Emp_job"] = "Support Enginner";
        rw3["Emp_Dep"] = "IT";
        dt.Rows.Add(rw3);
        DataRow rw4 = dt.NewRow();
        rw4["Emp_Name"] = "Arvind";
        rw4["Emp_id"] = "4678";
        rw4["Emp_job"] = "Sr Software Engineer";
        rw4["Emp_Dep"] = "IT";
        dt.Rows.Add(rw4);
        DataRow rw5 = dt.NewRow();
        rw5["Emp_Name"] = "Raja ram";
        rw5["Emp_id"] = "2300";
        rw5["Emp_job"] = "Test Engineer";
        rw5["Emp_Dep"] = "IT";
        dt.Rows.Add(rw5);
        GridVwRowColorchange.DataSource = dt;
        GridVwRowColorchange.DataBind();
    }
    protected void GridVwRowColorchange_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox headerchk = (CheckBox)GridVwRowColorchange.HeaderRow.FindControl("chkheader");
            CheckBox childchk = (CheckBox)e.Row.FindControl("chkchild");
            childchk.Attributes.Add("onclick", "javascript:Selectchildcheckboxes('" + headerchk.ClientID + "')");
            // mouse over color change
            e.Row.Attributes.Add("onmouseover", "self.MouseOverOldColor=this.style.backgroundColor;this.style.backgroundColor='#C0C0C0'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=self.MouseOverOldColor");
        }
    }
}

Gridview

0 comments:

Post a Comment