ASP.NET: Add paging to a repeater
From Wiki
Summary: An example of how we can add paging to a repeater control by using a PagedDataSource.
Need help with ASP.NET? Come and ask a question in our ASP.NET Forum
Data controls such as the GridView have built in support for paging, however this isn't the same for the Repeater control. To add this functionality, I've decided to use the PagedDataSource class.
Let's start by creating a simple page which just contains a Repeater:
- <%@ Page Language="VB" AutoEventWireup="false" CodeFile="RepeaterPaging.aspx.vb" Inherits="RepeaterPaging" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>Untitled Page</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Repeater ID="Repeater1" runat="server">
- <ItemTemplate>
- <h2><%#DataBinder.Eval(Container.DataItem, "Column1")%></h2>
- <p><%#DataBinder.Eval(Container.DataItem, "Column2")%></p>
- </ItemTemplate>
- </asp:Repeater>
- </div>
- </form>
- </body>
- </html>
Then, we'll populate this page with some sample data:
- Imports System.Data
- Partial Class RepeaterPaging
- Inherits System.Web.UI.Page
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- ' Check for a postback
- If Not Page.IsPostBack Then
- ' Bind the Repeater with some sample data
- Repeater1.DataSource = GetData()
- Repeater1.DataBind()
- End If
- End Sub
- Private Function GetData() As DataTable
- ' Declarations
- Dim dt As New DataTable
- Dim dr As DataRow
- ' Add some columns
- dt.Columns.Add("Column1")
- dt.Columns.Add("Column2")
- ' Add some test data
- For i As Integer = 0 To 10
- dr = dt.NewRow
- dr("Column1") = i
- dr("Column2") = "Some Text " & (i * 5)
- dt.Rows.Add(dr)
- Next
- ' Return the DataTable
- Return dt
- End Function
- End Class
As you'll see from the output, we have some sample data written out to the page ranging from items 0 to 10. Now, to add the paging capabilities we can use the PagedDataSource. To do this, we'll amend the GetData() function to return a PagedDataSource and we'll add a DataView from the current DataTable to it. e.g.
- Private Function GetData() As PagedDataSource
- ' Declarations
- Dim dt As New DataTable
- Dim dr As DataRow
- Dim pg As New PagedDataSource
- ' Add some columns
- dt.Columns.Add("Column1")
- dt.Columns.Add("Column2")
- ' Add some test data
- For i As Integer = 0 To 10
- dr = dt.NewRow
- dr("Column1") = i
- dr("Column2") = "Some Text " & (i * 5)
- dt.Rows.Add(dr)
- Next
- ' Add a DataView from the DataTable to the PagedDataSource
- pg.DataSource = dt.DefaultView
- ' Return the DataTable
- Return pg
- End Function
If we ran this code now, we wouldn't actually see a difference in the results since we haven't actually set a page size yet. However, before we do this, we'll add some navigation links and a label to our page so we can see what page we are on and also be able to navigate to each page. Just under the Repeater control, add these controls:
- <div>
- <a id="lnkPrev" runat="server">Previous</a>
- <asp:Label ID="lblCurrentPage" runat="server"></asp:Label>
- <a id="lnkNext" runat="server">Next</a>
- </div>
Now, we need to actually apply the functionality that will allow the Repeater to be paged. To do this, we'll use querystring values to determine which page we need to show and also set a default just in case this page doesn't exist. Then, we can set the relevant page by setting the CurrentPageIndex property of our PagedDataSource and then rebind the grid. I've added comments to each section of the code so you should be able to see what steps we are taking along the way so let's go ahead and replace our current code with the new code that applies this functionality:
- Imports System.Data
- Partial Class RepeaterPaging
- Inherits System.Web.UI.Page
- Private currentPage As Integer = 1 ' Default page
- Private pgdArticles As New PagedDataSource
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- ' Make sure the user hasn't tampered with the querystring value
- If Integer.TryParse(Request.QueryString("Page"), New Integer) = True Then
- currentPage = Request.QueryString("Page")
- End If
- ' Get the requested list of articles and set the paging attributes
- pgdArticles = GetData()
- pgdArticles.AllowPaging = True
- pgdArticles.PageSize = 2
- ' Show the Current Page number
- lblCurrentPage.Text = "Page " & currentPage & " of " & pgdArticles.PageCount
- ' Add the next/previous link targets if the relevant pages exist
- If currentPage = 1 Then
- lnkPrev.Attributes.Add("style", "visibility:hidden")
- Else
- lnkPrev.Attributes.Add("style", "visibility:visible")
- lnkPrev.HRef = "RepeaterPaging.aspx?Page=" & (currentPage - 1)
- End If
- If currentPage = pgdArticles.PageCount Then
- lnkNext.Attributes.Add("style", "visibility:hidden")
- Else
- lnkNext.Attributes.Add("style", "visibility:visible")
- lnkNext.HRef = "RepeaterPaging.aspx?Page=" & (currentPage + 1)
- End If
- ' Bind the repeater
- BindData()
- End Sub
- Private Sub BindData()
- ' Make sure the user hasn't requested a page that doesn't exist
- If currentPage < 1 Then
- currentPage = 1
- End If
- If currentPage > pgdArticles.PageCount Then
- currentPage = pgdArticles.PageCount
- End If
- ' Set the current page to be shown
- pgdArticles.CurrentPageIndex = (currentPage - 1)
- ' Bind the repeater
- Repeater1.DataSource = pgdArticles
- Repeater1.DataBind()
- End Sub
- Private Function GetData() As PagedDataSource
- ' Declarations
- Dim dt As New DataTable
- Dim dr As DataRow
- Dim pg As New PagedDataSource
- ' Add some columns
- dt.Columns.Add("Column1")
- dt.Columns.Add("Column2")
- ' Add some test data
- For i As Integer = 0 To 10
- dr = dt.NewRow
- dr("Column1") = i
- dr("Column2") = "Some Text " & (i * 5)
- dt.Rows.Add(dr)
- Next
- ' Add a DataView from the DataTable to the PagedDataSource
- pg.DataSource = dt.DefaultView
- ' Return the DataTable
- Return pg
- End Function
- End Class
When we run this code, you'll now see that we now have 6 pages (since we set the page size to 2 in this example) and that you can navigate to each page by using the Previous/Next links.
This Hack is part of the ASP.NET Hacks collection


