Custom Paging Control for GridView using DropDownList Control in ASP.Net

12. December 2009

create aspx page
<div>

Jump To:

<asp:DropDownList ID="ddlJumpTo" runat="server"

    OnSelectedIndexChanged = "PageNumberChanged" AutoPostBack = "true">

</asp:DropDownList>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false"

    AllowPaging = "true" PageSize = "10" PagerSettings-Visible = "false">

    <Columns>

        <asp:BoundField DataField="ContactName" HeaderText="Contact Name" />

        <asp:BoundField DataField="City" HeaderText="City" />

        <asp:BoundField DataField="Country" HeaderText="Country" />

        <asp:BoundField DataField="PostalCode" HeaderText="Postal Code" />

    </Columns>

</asp:GridView>

</div>

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE PROCEDURE [dbo].[spx_GetCustomers]

       @TotalRows INT OUTPUT

AS

BEGIN

      SET NOCOUNT ON;

      SELECT ContactName, City, Country, PostalCode

      FROM Customers

      SELECT @TotalRows=COUNT(*)

      FROM CUSTOMERS

END

private int BindGrid(int CurrentPageNo)

{

    int TotalRows = 0;

    DataTable dt = new DataTable();

    String strConnString = System.Configuration.ConfigurationManager

                .ConnectionStrings["conString"].ConnectionString;

    SqlConnection con = new SqlConnection(strConnString);

    SqlDataAdapter sda = new SqlDataAdapter();

    SqlCommand cmd = new SqlCommand("spx_GetCustomers");

    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.Add("@TotalRows", SqlDbType.Int)

                .Direction = ParameterDirection.Output;

    cmd.Connection = con;

    sda.SelectCommand = cmd;

    sda.Fill(dt);

    TotalRows = (int)cmd.Parameters["@TotalRows"].Value;

    GridView1.PageIndex = CurrentPageNo - 1;

    GridView1.DataSource = dt;

    GridView1.DataBind();

    return TotalRows;

}


Populating the Page Number DropDownList

The following method populates the DropDownList that will hold the page numbers

C#

private void FillJumpToList(int TotalRows)

{

    int PageCount = this.CalculateTotalPages(TotalRows);

    for (int i = 1; i <= PageCount; i++)

    {

        ddlJumpTo.Items.Add(new ListItem(i.ToString(), i.ToString()));

    }

}

Calculating Count of Total Pages

The below method is used to get the count of total number of pages based using the GridView Page Size and the total records present in the database table.

C#

private int CalculateTotalPages(int intTotalRows)

{

    int intPageCount = 1;

    double dblPageCount = (double)(Convert.ToDecimal(intTotalRows)

                            / Convert.ToDecimal(GridView1.PageSize));

    intPageCount = Convert.ToInt32(Math.Ceiling(dblPageCount));

    return intPageCount;

}

Handling the Page Change

The page change event is handled using the OnSelectedIndexChanged event of the ASP.Net DropDownList control which calls the following event handler

C#

protected void PageNumberChanged(object sender, EventArgs e)

{

    int PageNo = Convert.ToInt32(ddlJumpTo.SelectedItem.Value);

    this.BindGrid(PageNo);

}


PageLoad Event

Finally here is the page load event of the ASP.Net page. It does two jobs

1. Populates the GridView

2. Populates the page Number DropDownList

C#

protected void Page_Load(object sender, EventArgs e)

{

    if (!IsPostBack)

    {

        int TotalRows = this.BindGrid(1);

        this.FillJumpToList(TotalRows);

    }

}


Author: Aamir Hasan     औथोर: आमिर हसन       أثر أمير حسن .

ALL, asp.net, asp.net 4.0, Javascript , , ,


Comments

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading





User Name: Guest

Your Ip: 38.107.191.91
Time: