DataList Paging

D

David

Hello.

How can I enable and manage paging capability on DataList control like
DataGrid?

Thank you.
 
M

Michael Nemtsev

Hello,

The trick when using a DataList to page data is to think about where
the paging occurs. Since the DataList does not support paging but
does support being bound to a DataSet. If it were simple the story
would end here. Unfortunately a DataSet does not support paging
either. So what you need to do is make your DataSet a single page of
data! This way you only retrieve the data you need for a single page.

See http://www.dotnetjunkies.com/Tutorial/70E24E50-C179-4563-B053-
2742516BF05B.dcik
 
J

Joe Fallon

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>&nbsp;&nbsp;&nbsp;&nbsp;")
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>&nbsp;&nbsp;")
Else
sb.Append("&nbsp;&nbsp;")
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>&nbsp;&nbsp;")
Else
sb.Append("<a href=")
sb.Append(DrawLink(x))
sb.Append(">")
sb.Append(x)
sb.Append("</a>&nbsp;&nbsp;")
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("&nbsp;&nbsp;")
End If

'draw the Next 10 if number range not more than total page count
If pageEnd < pageCount Then
sb.Append("&nbsp;&nbsp;&nbsp;&nbsp;<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
=========================================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top