Fork me on GitHub

AspNetPager 示例 - 使用GoToPage方法

该示例演示如何以编程方式动态指定要跳转的页面的索引,使用GoToPage方法引发分页事件并跳转到指定页面。
订单编号订单日期公司名称雇员姓名
110771998/5/6Rattlesnake Canyon GroceryNancy Davolio
110761998/5/6Bon app'Margaret Peacock
110751998/5/6Richter SupermarktLaura Callahan
110741998/5/6Simons bistroRobert King
110731998/5/5Pericles Comidas clásicasAndrew Fuller
110721998/5/5Ernst HandelMargaret Peacock
110711998/5/5LILA-SupermercadoNancy Davolio
110701998/5/5Lehmanns MarktstandAndrew Fuller
110691998/5/4Tortuga RestauranteNancy Davolio
110681998/5/4Queen CozinhaLaura Callahan

请输入要跳转到的页索引:  

GoToPage.aspx:

<%@ Page Title="使用GoToPage方法" Language="C#"  MetaDescription="该示例演示如何以编程方式动态指定要跳转的页面的索引,使用GoToPage方法引发分页事件并跳转到指定页面。" MasterPageFile="AspNetPager.master" AutoEventWireup="true" Inherits="CurrentPageIndex_Default" Codebehind="GoToPage.aspx.cs" %>

<asp:Content ID="Content2" ContentPlaceHolderID="main" Runat="Server">
         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" CssClass="table table-bordered table-striped">
            <Columns>
                <asp:BoundField DataField="orderid" HeaderText="订单编号" />
                <asp:BoundField DataField="orderdate" HeaderText="订单日期" DataFormatString="{0:d}" />
                <asp:BoundField DataField="companyname" HeaderText="公司名称" />
                <asp:BoundField DataField="employeename" HeaderText="雇员姓名" />
            </Columns>
        </asp:GridView>
        <webdiyer:AspNetPager ID="AspNetPager1" runat="server" Width="100%" ShowPageIndexBox="Always" TextBeforePageIndexBox="跳转到第" TextAfterPageIndexBox="页" 
        PageSize="10" CurrentPageIndex="39" FirstPageText="首页" LastPageText="尾页" PrevPageText="上页" NextPageText="下页" OnPageChanged="AspNetPager1_PageChanged">
        </webdiyer:AspNetPager><br />
    请输入要跳转到的页索引:<asp:TextBox ID="tb_pageindex" runat="server" Width="60px" Text="8"></asp:TextBox><asp:Button ID="Button1"
        runat="server" Text="转到" OnClick="Button1_Click" /><asp:RequiredFieldValidator
        ID="RequiredFieldValidator1" runat="server" Text="必须输入页索引" ControlToValidate="tb_pageindex"/><asp:CompareValidator
            ID="CompareValidator1" runat="server" ControlToValidate="tb_pageindex" Operator="DataTypeCheck" Type="Integer" Text="页索引必须是整数"/><asp:Label ID="lbl_error" runat="server" ForeColor="Red" EnableViewState="false"></asp:Label>
</asp:Content>

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 += "&nbsp;&nbsp;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 = "输入的页索引格式不正确";
        }
    }
}