If you would like to hire my services, you can now do so by visiting the following link:
Website Design Darlington
Article Statistics
Code Bank Statistics
Summary: An example of how we can add paging to a repeater control by using a PagedDataSource.
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.
Posted on 03/05/2007 03:00:51
1. Dimitar Voynov 07/06/2007 00:09:28
Nice!
2. Kooshal 08/01/2008 21:05:13
Great!
3. ishwor 21/07/2008 02:07:28
Error:lblCurrentPage,lblnext,lblprev does not exits in corrent context. for second one
Please keep your comments relevant to this page. Any inappropriate or purely promotional comments may be removed. Email addresses are never displayed but are required so you can confirm your comments.