Paging With XPO in ASP.NET

I just saw this post in the DevExpress XPO forum: ASP.Net XpoDataSource with standard ASP.Net Repeater. Specifically, the question is how a simple setup with XPO and the standard ASP.NET Repeater can support paging. Now, the following setup may not be optimal in all cases, because it depends on an XPCollection and may therefore have more overhead than you’d optimally want. But I still wanted to start by documenting the built-in support, so here goes. (You can skip the following descriptions and just grab the sample from the bottom of this post if you want.)

In my web form, I have the following code:

<asp:Repeater ID="dataRepeater" runat="server">
    <b><%# DataBinder.Eval(Container.DataItem, "Name")%></b>
<asp:Repeater ID="pageListRepeater" runat="server">
    <a href="default.aspx?page=<%# Container.DataItem %>"><%# Container.DataItem %></a> 

The first repeater is the one that outputs information for each of the objects in my source collection. The second one is used with a range (from code, below) to display links to the pages in my collection. My page has these two methods:

protected void Page_Load(object sender, EventArgs e) {
  using (var session = new Session()) {
    var data = new XPCollection<Person>(session);
    var pageable = new XPPageSelector(data);
    pageable.PageSize = 3;

    pageListRepeater.DataSource = Enumerable.Range(1, pageable.PageCount);
    pageListRepeater.DataBind( );

    int currentPage = GetCurrentPage() - 1;
    pageable.CurrentPage = currentPage;
    dataRepeater.DataSource = pageable;
    dataRepeater.DataBind( );

int GetCurrentPage( ) {
  int page;
  if (int.TryParse(Request.Params["page"], out page))
    return page;
  return 1;

As you can see, the XPPageSelector does all the heavy lifting. It is initialized with an XPCollection and a page size (set to 3 in my case because I don’t have very much test data) and it returns the number of pages of that size in its PageCount property. This is then used to configure the repeater that renders the page links. Finally the current page can be retrieved from request parameters, and with that info the XPPageSelector works as a collection for the repeater that shows the data. Easy, eh?

Here’s the download of the sample application:

Have fun!

Sorry, this blog does not support comments.

I used various blog hosting services since this blog was established in 2005, but unfortunately they turned out to be unreliable in the long term and comment threads were lost in unavoidable transitions. At this time I don't want to enable third-party services for comments since it has become obvious in recent years that these providers invariably monetize information about their visitors and users.

Please use the links in the page footer to get in touch with me. I'm available for conversations on Keybase, Matrix, Mastodon or Twitter, as well as via email.