Click here to Skip to main content
15,868,440 members
Articles / Web Development / ASP.NET
Article

Paging with Repeater control in ASP.NET

Rate me:
Please Sign up or sign in to vote.
2.80/5 (43 votes)
8 Oct 2004 278K   48   40
Paging with Repeater control in ASP.NET.

Introduction

Repeater and DataList controls offer a quick and flexible means of displaying data on a ASPX page. But they offer no paging functionality built in. The DataGrid control has in-built paging but its structure is more rigid. There are several articles on various ASP.NET Resource Sites that offer solutions, but I'm going to show you how to use the PagedDataSource class.

The PagedDataSource class encapsulates the properties of the DataGrid control that allow it to perform paging. But we can use the class with Repeater and DataList controls to perform paging in much the same way as a DataGrid.

VB
Private Sub Page_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    Dim myConnection As New _
      SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
    Dim myDA As New SqlClient.SqlDataAdapter("Select * from dtsDefect", _
                                                             myConnection)
    myDA.Fill(ds, "t1")
    pageds.DataSource = ds.Tables("t1").DefaultView
    pageds.AllowPaging = True
    pageds.PageSize = 4
    Dim curpage As Integer

    If Not IsNothing(Request.QueryString("Page")) Then
        curpage = Convert.ToInt32(Request.QueryString("Page"))
    Else
        curpage = 1
    End If

    pageds.CurrentPageIndex = curpage - 1
    lblCurrpage.Text = "Page: " + curpage.ToString()

    If Not pageds.IsFirstPage Then
        lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + _
                                       "?Page=" + CStr(curpage - 1)
    End If

    If Not pageds.IsLastPage Then
        lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + _
                                       "?Page=" + CStr(curpage + 1)
    End If

    Repeater1.DataSource = pageds
    Repeater1.DataBind()
End Sub

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalpageds Pin
taher ben taib14-Jun-05 22:40
taher ben taib14-Jun-05 22:40 
GeneralRe: pageds Pin
jresins20-Jul-05 16:16
jresins20-Jul-05 16:16 
GeneralRe: pageds Pin
Ivan (Zaragoza)14-Dec-08 9:08
Ivan (Zaragoza)14-Dec-08 9:08 
Generalproblem Pin
ayeletatary8-Mar-05 5:58
ayeletatary8-Mar-05 5:58 
GeneralRe: problem Pin
Pathik H Rawal17-Mar-05 22:39
Pathik H Rawal17-Mar-05 22:39 
GeneralRe: problem Pin
Anonymous25-Mar-05 6:00
Anonymous25-Mar-05 6:00 
GeneralRe: problem Pin
Pathik H Rawal27-Mar-05 17:45
Pathik H Rawal27-Mar-05 17:45 
GeneralRe: problem Pin
Anonymous28-Mar-05 18:26
Anonymous28-Mar-05 18:26 
Thanks for reply.
Sorry! I still cannot this paging to work, can you post the all web controls for this code to work, I am very new to this .NET, therefore it's hard for me to understand: here is my code, could you please help me to make it work, I dont know how to create control for "pageds". Thank you vey much!

<%@ Import Namespace="System.Data" %>
<%@ import namespace="System.Data.OleDb" %>

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dim dbconn,sql,dbcomm,dbread
Dim strConnection As String
strConnection = ConfigurationSettings.AppSettings("DNS")
dbconn=New OleDbConnection(strConnection)
dbconn.Open()
sql="SELECT ID, strTitle FROM tblTruyenThieuNhi ORDER BY strTitle Asc;"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
Dim ds As DataSet()
dbread.Fill(ds,"ID")
dbread.Fill(ds,"strTitle")

pageds.DataSource = ds.Tables("ID").DefaultView
pageds.AllowPaging = True
pageds.PageSize = 4

Dim curpage As Integer

If Not IsNothing(Request.QueryString("Page")) Then
curpage = Convert.ToInt32(Request.QueryString("Page"))
Else
curpage = 1
End If

pageds.CurrentPageIndex = curpage - 1
'lblCurrpage.Text = "Page: " + curpage.ToString()

If Not pageds.IsFirstPage Then
Prev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + CStr(curpage - 1)

End If

If Not pageds.IsLastPage Then
Next.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + CStr(curpage + 1)
End If

Repeater1.DataSource = pageds
Repeater1.DataBind()

End Sub

Sub Page_Repeater(sender As Object, e As CommandEventArgs)
'will be define later
End Sub







<asp:repeater id="Repeater1" runat="server">
<headertemplate>



<itemtemplate>



<alternatingitemtemplate>



<footertemplate>

<asp:linkbutton id="LinkButton1"
text="<%#Container.DataItem("strTitle")%>"
commandname="LinkButton1"
commandargument="<%#Container.DataItem("ID")%>"
oncommand="LinkButton1_Command" runat="server">

<asp:linkbutton id="LinkButton1"
text="<%#Container.DataItem("strTitle")%>"
commandname="LinkButton1"
commandargument="<%#Container.DataItem("ID")%>"
oncommand="LinkButton1_Command" runat="server">

<asp:linkbutton id="Prev" text="<< Previous" onclick="Page_Repeater" runat="server">  
<asp:linkbutton id="Next" text="Next >>" onclick="Page_Repeater" runat="server">




GeneralRe: problem Pin
Anonymous28-Mar-05 18:38
Anonymous28-Mar-05 18:38 
GeneralRe: problem Pin
Anonymous29-Mar-05 13:38
Anonymous29-Mar-05 13:38 
GeneralRe: problem Pin
zubairuddin19-Jun-05 19:49
zubairuddin19-Jun-05 19:49 
QuestionCan we use any templates when we display data? Pin
gun4boy20-Feb-05 21:36
gun4boy20-Feb-05 21:36 
AnswerRe: Can we use any templates when we display data? Pin
Pathik H Rawal23-Feb-05 16:45
Pathik H Rawal23-Feb-05 16:45 
GeneralRe: Can we use any templates when we display data? Pin
nguyenlinhhaivt17-Sep-06 21:28
nguyenlinhhaivt17-Sep-06 21:28 
GeneralRe: Can we use any templates when we display data? Pin
nguyenlinhhaivt17-Sep-06 21:41
nguyenlinhhaivt17-Sep-06 21:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.