Hosting Articles
Paging with PagedDataSource andRepeater Control in ASP.Net
Repeater control offer a fast and flexible proceeds of displaying data on an ASPX page. But it offer no paging functionality built in. The DataGrid control has integral paging but its configuration is stiffer. Im 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 control to perform paging in much the same way as a DataGrid. We need to add some code to the ASPX page; you can view code as mentioned as below.
New
Imports system.data.sqlclient
Open database connection
Dim
intpage
As
Integer
Dim
last
As
String
Query = "select query”
Dim
myDA As
New SqlClient.SqlDataAdapter (query, SQL Connection string)
Dim
ds as
New DataSet()
myDA.Fill (ds, "t1")
Dim
pageds As
New PagedDataSource
pageds.DataSource = ds.Tables (0).DefaultView
pageds.AllowPaging = True
pageds.PageSize = 10
If
Not
IsNothing (Request.QueryString ("page")) Then
Intpage = Convert.ToInt32 (Request.QueryString ("page")) - 1
If
intpage < 0 Then
Intpage = 0
End If
If (intpage > pageds.PageCount)
Then
Intpage = pageds.PageCount - 1
End If
pageds.CurrentPageIndex = intpage
End
If
Literal3.Text += ("<b>")
Literal3.Text += ("RESULT PAGES: ")
Literal3.Text += ("</b>")
If
Not pageds.CurrentPageIndex = 0 Then
Literal3.Text +="<a href = Page URL” & "&page=1"
Literal3.Text += ">"
Literal3.Text += "First"
Literal3.Text += "</a>"
Literal3.Text += "<a href = Page URL"
Literal3.Text += ">"
Literal3.Text += "Previous"
Literal3.Text += "</a>"
End If
Literal2.Text = CreatePagerLinks (pageds, "Page URL")
If Not pageds.IsLastPage then
Literal2.Text +="<a href=page URL"&"&page="_
& pageds.CurrentPageIndex + 2
Literal2.Text += ">"
Literal2.Text += "Next"
Literal2.Text += "</a>"
last = pageds.PageCount
Literal2.Text += "<a href=Page URL" & "&page=" & last & ""
Literal2.Text += ">"
Literal2.Text += "Last"
Literal2.Text += "</a>"
End If
If pageds.PageCount = 1 Then
Literal2.Visible = False
End If
Repeater1.DataSource = pageds
Repeater1.DataBind ()
Close connection
End
Sub
Function CreatePagerLinks (ByVal pageds As PagedDataSource, ByVal BaseUrl as String) As String
Dim
sbpager As New System.Text.StringBuilder
If Not (pageds.IsFirstPage) then
sbpager.Append ("<a href=")
sbpager.Append (BaseUrl)
sbpager.Append ("</a>")
If Not (pageds.CurrentPageIndex = 1) Then
sbpager.Append ("<a href=")
sbpager.Append (BaseUrl)
sbpager.Append ("&page=")
sbpager.Append (pageds.CurrentPageIndex.ToString ())
sbpager.Append ("</a>")
End If
End If
Dim intLow = pageds.CurrentPageIndex - 1
Dim intHigh = pageds.CurrentPageIndex
If (intLow < 1) Then
IntLow = 1
End If
If intHigh > 1 Then
IntHigh = 1
End If
If (intHigh > pageds.PageCount) Then
IntHigh = pageds.PageCount
End If
If (intHigh - intLow < 10) Then
While ((intHigh < intLow + 9) and intHigh < pageds.PageCount)
ntHigh = intHigh + 1
End While
End If
If (intHigh - intLow < 10) Then
While ((intLow > intHigh - 9) and intLow > 1)
IntLow = intLow - 1
End While
End If
Dim x as Integer
For x = intLow to intHigh
If (x = pageds.CurrentPageIndex + 1) Then
sbpager.Append (x.ToString () + " ")
Else
sbpager.Append ("<a href=")
sbpager.Append (BaseUrl)
sbpager.Append ("&page=")
sbpager.Append (x.ToString ())
sbpager.Append (">")
sbpager.Append (x.ToString ())
sbpager.Append ("</a>")
End If
Next
If Not (pageds.IsLastPage) then
If Not ((pageds.CurrentPageIndex+1) =pageds.PageCount) Then
sbpager.Append ("<a href=")
sbpager.Append (BaseUrl)
sbpager.Append ("&page=")
sbpager.Append (Convert.ToString (pageds.CurrentPageIndex + 1))
sbpager.Append ("></a>")
End If
sbpager.Append ("<a href=")
sbpager.Append (BaseUrl)
sbpager.Append ("&page=")
sbpager.Append (pageds.PageCount.ToString ())
sbpager.Append ("></a>")
End If
Return sbpager.ToString ()
End Function
This is the full code for ASP.Net Paging with PagedDataSource and Repeater control.