Fork me on GitHub

AspNetPager demo - Using page index box

This sample demonstrates how to config AspNetPager control to allow users input or select page index they want to jump to.

Order ID: 11074     Order Date: 5/6/1998
Company Name: Simons bistro
Employee Name: Robert King

Order ID: 11075     Order Date: 5/6/1998
Company Name: Richter Supermarkt
Employee Name: Laura Callahan

Order ID: 11076     Order Date: 5/6/1998
Company Name: Bon app'
Employee Name: Margaret Peacock

Order ID: 11077     Order Date: 5/6/1998
Company Name: Rattlesnake Canyon Grocery
Employee Name: Nancy Davolio

Order ID: 11070     Order Date: 5/5/1998
Company Name: Lehmanns Marktstand
Employee Name: Andrew Fuller

Order ID: 11071     Order Date: 5/5/1998
Company Name: LILA-Supermercado
Employee Name: Nancy Davolio

Order ID: 11072     Order Date: 5/5/1998
Company Name: Ernst Handel
Employee Name: Margaret Peacock

Order ID: 11073     Order Date: 5/5/1998
Company Name: Pericles Comidas clásicas
Employee Name: Andrew Fuller

Order ID: 11067     Order Date: 5/4/1998
Company Name: Drachenblut Delikatessen
Employee Name: Nancy Davolio

Order ID: 11068     Order Date: 5/4/1998
Company Name: Queen Cozinha
Employee Name: Laura Callahan

Order ID: 11069     Order Date: 5/4/1998
Company Name: Tortuga Restaurante
Employee Name: Nancy Davolio

Order ID: 11064     Order Date: 5/1/1998
Company Name: Save-a-lot Markets
Employee Name: Nancy Davolio

Order ID: 11065     Order Date: 5/1/1998
Company Name: LILA-Supermercado
Employee Name: Laura Callahan

Order ID: 11066     Order Date: 5/1/1998
Company Name: White Clover Markets
Employee Name: Robert King

Order ID: 11060     Order Date: 4/30/1998
Company Name: Franchi S.p.A.
Employee Name: Andrew Fuller

Order ID: 11061     Order Date: 4/30/1998
Company Name: Great Lakes Food Market
Employee Name: Margaret Peacock

<<<12345678910...>>>  Go to page

PageIndexBox.aspx:

    <asp:DataList ID="DataList1" runat="server"  RepeatDirection="Horizontal" RepeatColumns="2" Width="100%">
        <ItemStyle Width="50%"/>
<ItemTemplate>
Order ID: <%#DataBinder.Eval(Container.DataItem,"orderid")%>    
Order Date: <font color="red"><%#DataBinder.Eval(Container.DataItem,"orderdate","{0:d}")%></font><br>
Company Name: <%#DataBinder.Eval(Container.DataItem,"companyname")%><br>
Employee Name: <%#DataBinder.Eval(Container.DataItem,"employeename")%><br>
<hr>
</ItemTemplate>
        </asp:DataList>
    <webdiyer:aspnetpager id="AspNetPager1" runat="server" horizontalalign="Center" onpagechanged="AspNetPager1_PageChanged" ShowPageIndexBox="Always" PageIndexBoxType="DropDownList" TextBeforePageIndexBox="Go to page" PageSize="16"
        width="100%"></webdiyer:aspnetpager>
    <asp:RadioButtonList ID="rbl_boxtype" runat="server" AutoPostBack="True" OnSelectedIndexChanged="rbl_boxtype_SelectedIndexChanged" RepeatDirection="Horizontal" RepeatLayout="Flow">
    <asp:ListItem>TextBox</asp:ListItem>
    <asp:ListItem Selected="true">DropDownList</asp:ListItem>
    </asp:RadioButtonList>

PageIndexBox.aspx.cs:

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

public partial class PageIndexBox_Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int totalOrders = (int)SqlHelper.ExecuteScalar(CommandType.StoredProcedure, "P_GetOrderNumber");
            AspNetPager1.RecordCount = totalOrders;
            bindData();
        }
    }

    void bindData()
    {
        DataList1.DataSource = SqlHelper.ExecuteReader(CommandType.StoredProcedure, ConfigurationManager.AppSettings["pagedSPName"],
            new SqlParameter("@startIndex", AspNetPager1.StartRecordIndex),
            new SqlParameter("@endIndex", AspNetPager1.EndRecordIndex));
        DataList1.DataBind();
    }
    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        bindData();
    }
    protected void rbl_boxtype_SelectedIndexChanged(object sender, EventArgs e)
    {
        AspNetPager1.PageIndexBoxType = (rbl_boxtype.SelectedIndex == 0)
                                            ? PageIndexBoxType.TextBox
                                            : PageIndexBoxType.DropDownList;
    }
}