Friday 18 July 2014

how to Maintain State of the Checkboxes during paging in Gridview in Asp.Net







Description:-

In this example we explain that how to maintain the state of the checkbox while paging in gridview or when any control is postback.

The real problem is that I want to delete the checked or selected rows in gridview.if I was select first three rows of the gridview's first page and then navigate to second gridview page and select first two row and then click on delete button then all selected or checked rows are deleted but that not happen only second page's selected rows are deleted because first page checked row's state does not maintain gridview because of postback. So in this type of situation we have to define another code as below.

I have faced real problem that I have one gridview with checkbox and paging and every griview pages display Five rows per page. Suppose I have select or checked first three rows in first gridview page using checkbox and now I was transfer to second gridview page and checked last two rows using checkbox then again if I was transfer to first gridview page then first three rows of the gridview must be checked. But that situation not happens because gridview cannot maintain the state if control is postback so due to this reason I was found a solution are below.




How to Send Forgot Password to user Email Send Forgot Password to user email

Rotate the Ads witout Refreshing the webpage Rotate the Advertisement in Asp.Net

Export Gridview Data to PDF file Export Grid data to PDF File



Default2.appx:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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 id="Head1" runat="server">
    <title>how to Maintain State of the Checkboxes during paging in Gridview in Asp.Net
    </title>
</head>
<body>
    <form id="form2" runat="server">
    <div>
        <asp:GridView runat="server" ID="grd_employee" AllowPaging="true" AllowSorting="true"
            AutoGenerateColumns="false" OnPageIndexChanging="grd_employee_PageIndexChanging"
            PageSize="10" DataKeyNames="Emp_Id">
            <RowStyle BackColor="#EFF3FB" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:TemplateField HeaderText="Select">
                    <ItemTemplate>
                        <asp:CheckBox ID="chkSelect" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Emp_Id" HeaderText="EmployeeId" />
                <asp:BoundField DataField="Emp_Name" HeaderText="EmployeeName" />
                <asp:BoundField DataField="Emp_Designation" HeaderText="Designation" />
                <asp:BoundField DataField="Emp_salary" HeaderText="Salary" />
                <asp:BoundField DataField="Emp_Country" HeaderText="Country" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>


Default2.appx.cs:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindEmployee_Griddata();
        }
    }
    //This method is used to bind the gridview
    protected void BindEmployee_Griddata()
    {
        SqlConnection con = new SqlConnection("Data Source=kiritgridview;Integrated Security=true;Initial Catalog=kiritDB");
        string query = "select * from Employees";
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        con.Open();
        da.SelectCommand = cmd;
        cmd.ExecuteNonQuery();
        da.Fill(ds);
        grd_employee.DataSource = ds;
        grd_employee.DataBind();
    }
    protected void grd_employee_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GetCheckBoxCheckValuelist();
        grd_employee.PageIndex = e.NewPageIndex;
        BindEmployee_Griddata();
        RenderCheckboxcheckvalues();
    }
    //This function are used to Render the checked checkbox values
    private void RenderCheckboxcheckvalues()
    {
        ArrayList employeedetail = (ArrayList)Session["CHECKED_ITEMS"];
        if (employeedetail != null && employeedetail.Count > 0)
        {
            foreach (GridViewRow gvrow in grd_employee.Rows)
            {
                int index = (int)grd_employee.DataKeys[gvrow.RowIndex].Value;
                if (employeedetail.Contains(index))
                {
                    CheckBox myCheckBox = (CheckBox)gvrow.FindControl("chkSelect");
                    myCheckBox.Checked = true;
                }
            }
        }
    }
    //This method is used to save the checkedstate of values
    private void GetCheckBoxCheckValuelist()
    {
        ArrayList employeedetail = new ArrayList();
        int index = -1;
        foreach (GridViewRow gvrow in grd_employee.Rows)
        {
            index = (int)grd_employee.DataKeys[gvrow.RowIndex].Value;
            bool result = ((CheckBox)gvrow.FindControl("chkSelect")).Checked;

            // Check in the Session
            if (Session["CHECKED_ITEMS"] != null)
                employeedetail = (ArrayList)Session["CHECKED_ITEMS"];
            if (result)
            {
                if (!employeedetail.Contains(index))
                    employeedetail.Add(index);
            }
            else
                employeedetail.Remove(index);
        }
        if (employeedetail != null && employeedetail.Count > 0)
            Session["CHECKED_ITEMS"] = employeedetail;
    }
}

0 comments:

Post a Comment