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.