Fork me on GitHub

AspNetPager demo - Paging for XML data source

This sample demonstrates how to use AspNetPager to page through data that stored in XML file.

Order IDOrder DateCompany NameCustomer IDEmployee Name
11077 5/5/1998 Rattlesnake Canyon Grocery RATTC Nancy Davolio
11076 5/5/1998 Bon app' BONAP Margaret Peacock
11075 5/5/1998 Richter Supermarkt RICSU Laura Callahan
11074 5/5/1998 Simons bistro SIMOB Robert King
11073 5/4/1998 Pericles Comidas clásicas PERIC Andrew Fuller
11072 5/4/1998 Ernst Handel ERNSH Margaret Peacock
11071 5/4/1998 LILA-Supermercado LILAS Nancy Davolio
11070 5/4/1998 Lehmanns Marktstand LEHMS Andrew Fuller
11069 5/3/1998 Tortuga Restaurante TORTU Nancy Davolio
11068 5/3/1998 Queen Cozinha QUEEN Laura Callahan

XmlData.aspx:

<asp:Repeater ID="Repeater1" runat="server">
    <headertemplate>
        <table width="100%" class="table table-bordered">
            <tr><th style="width:15%">Order ID</th><th style="width:15%">Order Date</th><th style="width:30%">Company Name</th><th style="width:20%">Customer ID</th><th style="width:20%">Employee Name</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, "companyname")%></td>
            <td><%#DataBinder.Eval(Container.DataItem,"customerid")%></td>
            <td><%#DataBinder.Eval(Container.DataItem,"employeename")%></td>
        </tr>
    </itemtemplate>
    <footertemplate>
        </table>
    </footertemplate>
</asp:Repeater>
<webdiyer:aspnetpager id="AspNetPager1" runat="server" horizontalalign="Center" onpagechanged="AspNetPager1_PageChanged"
                      UrlPaging="true" width="100%"></webdiyer:aspnetpager>

XmlData.aspx.cs:

using System;
using System.Data;
using System.Web.Caching;
using System.Web.UI;

public partial class XMLData_Default : Page
{
            const string cacheKey = "ordersTable";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable tbl = loadFromXMLOrCache();
            AspNetPager1.RecordCount = tbl.Rows.Count;
        }
    }

    DataTable loadFromXMLOrCache()
    {
        DataTable tbl = (DataTable)Cache[cacheKey];
        if (null == tbl)
        {
            string xmlFile = Server.MapPath("~/app_data/orders.xml");
            tbl = new DataTable();
                tbl.ReadXml(xmlFile);
            CacheDependency dep = new CacheDependency(xmlFile);
            Cache.Insert(cacheKey, tbl, dep);
        }
        return tbl;
    }

    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        DataTable tbl= loadFromXMLOrCache();
        if (tbl != null && tbl.Rows.Count > 0)
        {
            DataView dv = tbl.DefaultView;
            dv.Sort = "orderid desc";
            DataTable srcTbl = tbl.Clone();
            for (int i = AspNetPager1.StartRecordIndex; i <= AspNetPager1.EndRecordIndex; i++)
            {
                srcTbl.ImportRow(dv[i-1].Row);
            }
            Repeater1.DataSource = srcTbl;
            Repeater1.DataBind();
        }
    }
}