Web Design Nottingham Header

Linq and Paging

Publication Date: Fri 27 Jun 2008

Whilst working on a website, we had to implement paging on a list of images, so you could browse thumbnails. The list of images is never going to be very large, so we wondered if we could utilise Linq to achieve this.

We first created a repeater with an image in the ItemTemplate, something like this:

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
    <img src='Uploads\Thumbs\<%#Eval("Thumbnail") %>' width="100" />
    </ItemTemplate>
</asp:Repeater>

We then found some nice Linq features (Skip & Take). These allow you to skip the first number of items and take the specified next items. We stored the current index in ViewState and then bound the repeater to Linq code like this:

using (DataClassesDataContext context = new DataClassesDataContext())
{
            List<Portfolio> portfolios = (from p in context.Portfolios
                                                 where p.IsVisible == true
                                                 orderby p.Priority descending
                                                 select p).ToList();

            IEnumerable<Portfolio> ports = portfolios.Skip(int.Parse(ViewState["skip"].ToString())).Take(pageSize);

            this.Repeater1.DataSource = ports;
            this.Repeater1.DataBind();
}

We have further added to this code by disabling/enabling the navigation buttons at appropriate points. We would not recommend this solution for large collections as it is in memory paging which will not perform as well as SQL paging, but for small collections it works great as a quick solution.

As a further addition we wrapped the repeater and navigation buttons in an Ajax update panel, which gives the user a better experience as no postbacks happen when the user pages the images.

Quotes

"Very pleased with the website, simple administration, will be recommending Agilis Software"
Paul - Labarde.com

Latest Projects

Angel Wings web design Labarde web design Internet Gardener web design Stonesilver web design Build Investment Jewellery web design

Developer Certifications

MCPD logo

Partners

Comodo logo protx logo

Coding Standards

XHTML CSS

Agilis Software Limited | 2 Lynemouth Court, Arnold, Nottingham. NG5 8TY
Registered in England No. 5688723. VAT No. 886 4538 70
Tel: 07973 766989
Email: stephen@agilissoftware.co.uk