As you probably know the DataList control does not come with an inbuilt paging mechanism like the Grid. But! you can make great use of the 'PagedDataSource' class.
I needed control over a DataList in an image gallery project so I used Mr. Google and found lots of stuff out there on this but all were using the QueryString which was not available to me as this project was all Ajax enabled. I also wanted to show a link to all the page numbers [1 2 3..etc] as well as first, previous, next and last.
Here's the code.....
Public
NoOfPages As Integer
Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
MyBase.OnInit(e)
AddHandler rptPages.ItemCommand, AddressOf rptPages_ItemCommand
End Sub
Private
Sub BindAlbum(ByVal AlbumID As Integer)
lblPicComment.Text =
""
Dim _ImagesDB As ImagesDB = New ImagesDB
Dim dsImages As DataSet
dsImages = _ImagesDB.GetImagesByAlbumID(AlbumID)
Dim objPds As PagedDataSource = New PagedDataSource
objPds.DataSource = dsImages.Tables(0).DefaultView
objPds.AllowPaging =
True
objPds.PageSize = 24
Dim TotalpageSize As Integer = Int32.Parse(dsImages.Tables(0).Rows.Count)
NoOfPages = objPds.PageCount
lblInfo.Text =
"<b>" & TotalpageSize & "</b> images - Page <b>" & PageID & "</b> of <b>" & NoOfPages & "</b>"
LBLast.Enabled = IIf((Session(
"paged") = NoOfPages), False, True)
LBNext.Enabled = IIf((Session(
"paged") = NoOfPages), False, True)
LBPrev.Enabled = IIf((Session(
"paged") = 1), False, True)
LBFirst.Enabled = IIf((Session(
"paged") = 1), False, True)
Dim CurPage As Integer
If Session("Paged") > 0 Then
CurPage = Convert.ToInt32(Session(
"Paged"))
Else
CurPage = 1
End If
objPds.CurrentPageIndex = CurPage - 1
DLImages.DataSource = objPds
DLImages.DataBind()
If objPds.PageCount > 1 Then
rptPages.Visible =
True
Dim pages As New ArrayList()
Dim i As Integer = 0
While i < objPds.PageCount
pages.Add((i + 1).ToString())
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
End While
rptPages.DataSource = pages
rptPages.DataBind()
Else
rptPages.Visible =
False
End If
BindLatestPic(AlbumID)
End Sub
Protected
Sub LBFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LBFirst.Click
Session(
"paged") = 1
BindAlbum(Session(
"AlbumID"))
End Sub
Protected Sub LBPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LBPrev.Click
Session(
"paged") = Session("paged") - 1
BindAlbum(Session(
"AlbumID"))
End Sub
Protected Sub LBNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LBNext.Click
Session(
"paged") = Session("paged") + 1
BindAlbum(Session(
"AlbumID"))
End Sub
Protected Sub LBlast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LBLast.Click
Session(
"paged") = NoOfPages
BindAlbum(Session(
"AlbumID"))
End Sub
Sub rptPages_ItemCommand(ByVal source As Object, ByVal e As RepeaterCommandEventArgs)
Session(
"paged") = Convert.ToInt32(e.CommandArgument)
BindAlbum(Session(
"AlbumID"))
End Sub
Private
ReadOnly Property PageID() As Integer
Get
If (Not String.IsNullOrEmpty(Session("paged"))) Then
Return Convert.ToInt32(Session("paged"))
Else
Return 0
End If
End Get
End Property
<
div class="PagerDiv">
<asp:Label ID="lblInfo" runat="server">Rows</asp:Label>
<
asp:LinkButton ID="LBFirst" runat="server"><< First</asp:LinkButton>
<
asp:LinkButton ID="LBPrev" runat="server">< Previous</asp:LinkButton>
<
asp:Repeater ID="rptPages" Runat="server">
<
HeaderTemplate>[</HeaderTemplate>
<
ItemTemplate>
<
asp:LinkButton ID="btnPage" CommandName="Page" CommandArgument="<%#Container.DataItem %>" CssClass="text"
Text
="<%# Container.DataItem %>" Runat="server"></asp:LinkButton>
</
ItemTemplate>
<
FooterTemplate>]</FooterTemplate>
</
asp:Repeater>
<
asp:LinkButton ID="LBNext" runat="server">Next ></asp:LinkButton>
<
asp:LinkButton ID="LBLast" runat="server">Last >></asp:LinkButton>
</
div>
and here's the result...
