There is class named PagedDataSource that can be used to build custom paging
for the datalist and/or repeater.
Here is some sample code that shows how to use it:
=========================================================================
'declare some variables at the top of the page:
Protected pagedData As New PagedDataSource
Private mPageNo As Integer = 1
' add lblPages to your form where you want the page numbers to appear.
=========================================================================
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
If Request.QueryString("PageNo") Is Nothing Then
GetData()
doPaging()
Else
doPaging()
End If
End If
End Sub
=========================================================================
Sub GetData()
'get collection or dataset for the page here
End Sub
=========================================================================
Sub doPaging()
'use QueryString values for mPageNo
If Not Request.QueryString("PageNo") Is Nothing Then
mPageNo = CInt(Request.QueryString("PageNo"))
End If
'set the pagedData Properties
pagedData.CurrentPageIndex = mPageNo - 1
pagedData.DataSource = myCollection 'or Dataset
pagedData.AllowPaging = True
pagedData.PageSize = 5
'draw the page numbers as hyperlinks
lblPages.Text = DrawPaging(mPageNo, pagedData.PageCount)
myDataList.DataSource = pagedData
myDataList.DataBind()
End Sub
=========================================================================
Public Function DrawPaging(ByVal pageNumber As Integer, ByVal pageCount
As Integer) As String
Dim sb As New StringBuilder
Dim x, y, z, pageEnd, pageStart As Integer
If pageCount > 1 Then
'handle 10 at a time
'get the start and end
If pageNumber Mod 10 = 0 Then '10, 20, 30 appear with old set
pageStart = pageNumber - 9
Else
y = pageNumber.ToString.Length
If y = 1 Then '1 - 9
pageStart = 1
Else
z = CInt(Left(pageNumber.ToString, y - 1))
pageStart = (z * 10) + 1
End If
End If
If pageStart + 9 > pageCount Then
pageEnd = pageCount
Else
pageEnd = pageStart + 9
End If
'draw the Previous 10 if not in the first 10
If pageStart > 10 Then
sb.Append("<a href=")
sb.Append(DrawLink(pageStart - 1))
sb.Append(">Previous 10</a> ")
End If
'draw the previous button if not at the first item on the first page
If pageNumber <> 1 Then
sb.Append("<a href=")
sb.Append(DrawLink(pageNumber - 1))
sb.Append("><</a> ")
Else
sb.Append(" ")
End If
'draw the page numbers (current page is not a hyperlink it is in
bold in square brackets)
For x = pageStart To pageEnd
If x = pageNumber Then
sb.Append("<strong>[")
sb.Append(x)
sb.Append("]</strong> ")
Else
sb.Append("<a href=")
sb.Append(DrawLink(x))
sb.Append(">")
sb.Append(x)
sb.Append("</a> ")
End If
Next
'draw the next button if not at the last page
If pageNumber < pageCount Then
sb.Append("<a href=")
sb.Append(DrawLink(pageNumber + 1))
sb.Append(">></a>")
Else
sb.Append(" ")
End If
'draw the Next 10 if number range not more than total page count
If pageEnd < pageCount Then
sb.Append(" <a href=")
sb.Append(DrawLink(pageEnd + 1))
sb.Append(">Next 10</a>")
End If
sb.Append("<small><br>Total Page Count: ")
sb.Append(pageCount)
sb.Append("</small>")
sb.Append("</CENTER>")
Return sb.ToString
End If
End Function
=========================================================================
Private Function DrawLink(ByVal pageNumber As Integer) As String
Return "?PageNo=" & pageNumber
End Function
=========================================================================