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">
  <ItemTemplate>
    <b><%# DataBinder.Eval(Container.DataItem, "Name")%></b>
    <br></br>
  </ItemTemplate>
</asp:Repeater>
<asp:Repeater ID="pageListRepeater" runat="server">
  <ItemTemplate>
    <a href="default.aspx?page=<%# Container.DataItem %>"><%# Container.DataItem %></a> 
  </ItemTemplate>
</asp:Repeater>

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:

XpoAspNetRepeaterPaging.zip

Have fun!

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s