Fork me on GitHub

AspNetPager demo - Paging for AccessDataSource

This sample demonstrates how to use AspNetPager and AccessDataSource to page data records stored in Microsoft Office Access database.


Order IDOrder DateCustomer IDEmployee ID
11077 6/5/1996 RATTC 1
11076 6/5/1996 BONAP 4
11075 6/5/1996 RICSU 8
11074 6/5/1996 SIMOB 7
11073 6/4/1996 PERIC 2
11072 6/4/1996 ERNSH 4
11071 6/4/1996 LILAS 1
11070 6/4/1996 LEHMS 2
11069 6/3/1996 TORTU 1
11068 6/3/1996 QUEEN 8

AccessDataSource.aspx:

    <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="App_Data/NorthWnd.mdb"
        SelectCommand="select orderid,orderdate,customerid,employeeID from orders order by orderid desc" CacheKeyDependency="ADS_Orders" EnableCaching="True" OnSelected="AccessDataSource1_Selected"></asp:AccessDataSource>
    <br />
<asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
        <table width="100%" class="table table-bordered">
        <tr style="backGround-color:#CCCCFF"><th style="width:25%">Order ID</th><th style="width:25%">Order Date</th><th style="width:25%">Customer ID</th><th style="width:25%">Employee ID</th></tr>
        </HeaderTemplate>
        <ItemTemplate>
        <tr>
        <td><%#DataBinder.Eval(Container.DataItem,"orderid")%></td>
        <td><%#DataBinder.Eval(Container.DataItem,"orderdate","{0:d}")%></td>
        <td><%#DataBinder.Eval(Container.DataItem,"customerid")%></td>
        <td><%#DataBinder.Eval(Container.DataItem,"employeeid")%></td>
        </tr>
        </ItemTemplate>
        <FooterTemplate>
        </table>
        </FooterTemplate>
    </asp:Repeater>
    <webdiyer:aspnetpager id="AspNetPager1" runat="server" horizontalalign="Center" onpagechanged="AspNetPager1_PageChanged"
        width="100%"></webdiyer:aspnetpager>   

AccessDataSource.aspx.cs:

using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DataSources_PagedDataSrc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindData();
        }
    }

    void bindData()
    {
        PagedDataSource pds = new PagedDataSource();
        pds.AllowPaging = true;
        pds.PageSize = AspNetPager1.PageSize;
        pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
        DataView dv = (DataView)AccessDataSource1.Select(DataSourceSelectArguments.Empty);
        pds.DataSource = dv;
        Repeater1.DataSource = pds;
        Repeater1.DataBind();
    }
    
    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        bindData();
    }

    protected void AccessDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        AspNetPager1.RecordCount = e.AffectedRows;
    }
}