« ANN: Training Class "Business Apps with DXperience Silverlight"Slides and Samples from Basta 2010 »

Paging with XPO in ASP.NET

13/10/10

Permalink 09:44:38 pm
Categories: .NET

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:

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:

Code:

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!

No feedback yet

Leave a comment


Your email address will not be revealed on this site.
(Line breaks become <br />)
(For my next comment on this site)
(Allow users to contact me through a message form -- Your email will not be revealed!)
Please complete the song title below. Hint: enter 's', 'a', 't', 'i', 's', 'f', 'a', 'c', 't', 'i', 'o', 'n'
antispam test

Enter your email address:

Search

Oliver
MVP logo
May 2012
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31