Fork me on GitHub

AspNetPager demo - Paging within user control

This sample demonstrates how to use AspNetPager control within the user control.

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

Page 1 of 32  Order 1-10

default.aspx:

<%@ Register Src="PagerControl.ascx" TagName="PagerControl" TagPrefix="uc1" %>

 
 <uc1:PagerControl ID="PagerControl1" runat="server" />
    <asp:Button ID="Button1" runat="server" Text="PostBack" OnClick="Button1_Click" />
    <asp:Label ID="Label1" runat="server" EnableViewState="False"></asp:Label>

default.aspx.cs:

using System;
using System.Web.UI;

public partial class UserControl_Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Posted back";
    }
}

pagercontrol.ascx:

<%@ Control Language="C#" AutoEventWireup="true" Inherits="UserControl_PagerControl" Codebehind="PagerControl.ascx.cs" %>
<%@ Register TagPrefix="webdiyer" Namespace="Wuqi.Webdiyer" Assembly="AspNetPager"%>
        <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"
        showcustominfosection="Left" width="100%"  PageIndexBoxStyle="width:19px"
        CustomInfoHTML="Page  <font color='red'><b>%CurrentPageIndex%</b></font> of  %PageCount%  Order %StartRecordIndex%-%EndRecordIndex%"></webdiyer:aspnetpager>

pagercontrol.ascx.cs:

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

public partial class UserControl_PagerControl : UserControl
{
    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();
    }
}