Fork me on GitHub

AspNetPager demo - Reverse page index

This sample demonstrates how to reverse page index in url paging mode.

Page 1 of 32  Orders 1-10

Order IDOrder DateCompany NameEmployee Name
110745/6/1998Simons bistroRobert King
110755/6/1998Richter SupermarktLaura Callahan
110765/6/1998Bon app'Margaret Peacock
110775/6/1998Rattlesnake Canyon GroceryNancy Davolio
110705/5/1998Lehmanns MarktstandAndrew Fuller
110715/5/1998LILA-SupermercadoNancy Davolio
110725/5/1998Ernst HandelMargaret Peacock
110735/5/1998Pericles Comidas clásicasAndrew Fuller
110675/4/1998Drachenblut DelikatessenNancy Davolio
110685/4/1998Queen CozinhaLaura Callahan

Page 1 of 32  Orders 1-10

ReverseUrlPageIndex.aspx:

 <webdiyer:aspnetpager id="AspNetPager1" runat="server" horizontalalign="Center" PagingButtonSpacing="8px" onpagechanged="AspNetPager1_PageChanged"
            showcustominfosection="Right" urlpaging="True" width="100%" ShowNavigationToolTip="true" UrlPageIndexName="page" ReverseUrlPageIndex="true"></webdiyer:aspnetpager>
        <br /><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" CssClass="table table-bordered">
            <Columns>
                <asp:BoundField DataField="orderid" HeaderText="Order ID" />
                <asp:BoundField DataField="orderdate" HeaderText="Order Date" DataFormatString="{0:d}" />
                <asp:BoundField DataField="companyname" HeaderText="Company Name" />
                <asp:BoundField DataField="employeename" HeaderText="Employee Name" />
            </Columns>
        </asp:GridView>
        <br />
        <webdiyer:AspNetPager runat="server" ID="AspNetPager2" CloneFrom="aspnetpager1"></webdiyer:AspNetPager>

ReverseUrlPageIndex.aspx.cs:

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;

public partial class ReverseUrlPageIndex_Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //cache the number of total records to improve performance
            object obj = Cache[GetType()+"totalOrders"];
            if (obj == null)
            {
                int totalOrders = (int) SqlHelper.ExecuteScalar(CommandType.StoredProcedure, "P_GetOrderNumber");
                Cache[GetType()+"totalOrders"] = totalOrders;
                AspNetPager1.RecordCount = totalOrders;
            }
            else
            {
                AspNetPager1.RecordCount = (int) obj;
            }
        }
    }


    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        int startIndex = AspNetPager1.StartRecordIndex;
        if (AspNetPager1.CurrentPageIndex == AspNetPager1.PageCount)
            startIndex = AspNetPager1.RecordCount - AspNetPager1.PageSize+1;
        GridView1.DataSource = SqlHelper.ExecuteReader(CommandType.StoredProcedure, ConfigurationManager.AppSettings["pagedSPName"],
            new SqlParameter("@startIndex", startIndex),
            new SqlParameter("@endIndex", AspNetPager1.EndRecordIndex));
        GridView1.DataBind();
        AspNetPager1.CustomInfoHTML = "Page  <font color=\"red\"><b>" + AspNetPager1.CurrentPageIndex + "</b></font> of  " + AspNetPager1.PageCount;
        AspNetPager1.CustomInfoHTML += "  Orders " + AspNetPager1.StartRecordIndex + "-" + AspNetPager1.EndRecordIndex;
    }
    
}