Fork me on GitHub

AspNetPager demo - Using GoToPage method

This sample demonstrates how to use GoToPage method to jump to specified page and fire PageChanging and PageChanged events.

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

Enter new page index:  

GoToPage.aspx:

    <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>
        <webdiyer:AspNetPager ID="AspNetPager1" runat="server" Width="100%" ShowPageIndexBox="Always" TextBeforePageIndexBox="Go to page:"
        PageSize="10" CurrentPageIndex="39" FirstPageText="First" LastPageText="Last" PrevPageText="Prev" NextPageText="Next" OnPageChanged="AspNetPager1_PageChanged">
        </webdiyer:AspNetPager><br />
    Enter new page index:<asp:TextBox ID="tb_pageindex" runat="server" Width="60px" Text="8"></asp:TextBox><asp:Button ID="Button1"
        runat="server" Text="Go to page" OnClick="Button1_Click" /><asp:RequiredFieldValidator
        ID="RequiredFieldValidator1" runat="server" Text="required" ControlToValidate="tb_pageindex"/><asp:CompareValidator
            ID="CompareValidator1" runat="server" ControlToValidate="tb_pageindex" Operator="DataTypeCheck" Type="Integer" Text="page index must be an integer value"/><asp:Label ID="lbl_error" runat="server" ForeColor="Red" EnableViewState="false"></asp:Label>

GoToPage.aspx.cs:

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Wuqi.Webdiyer;

public partial class CurrentPageIndex_Default : System.Web.UI.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;
                obj = totalOrders;
            }
            AspNetPager1.RecordCount = (int) obj;
            bindData();
        }
    }

    void bindData()
    {
        GridView1.DataSource = SqlHelper.ExecuteReader(CommandType.StoredProcedure, ConfigurationManager.AppSettings["pagedSPName"],
            new SqlParameter("@startIndex", AspNetPager1.StartRecordIndex),
            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;
    }

    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        bindData();
    }
    protected void Button1_Click(object src,EventArgs e)
    {
        try
        {
            int pageindex = int.Parse(tb_pageindex.Text);
            AspNetPager1.GoToPage(pageindex);
        }
        catch(FormatException)
        {
            lbl_error.Text = "invalid page index";
        }
    }
}