iSkool.Net
Remember me 
[ Forgotten Password? ] [ Sign up ]
Blogs
jack
Blog Titles
Custom paging a datalist .Net 2.0 +
[ 5/5/2009 5:26:04 AM ]
Handling extensionless URLs
[ 6/24/2007 12:55:05 AM ]
Screen Shot Grabber
[ 3/3/2007 9:59:51 PM ]
Custom paging a datalist .Net 2.0 +

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">

&nbsp;&nbsp;<asp:Label ID="lblInfo" runat="server">Rows</asp:Label>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<asp:LinkButton ID="LBFirst" runat="server"><< First</asp:LinkButton>

<asp:LinkButton ID="LBPrev" runat="server">< Previous</asp:LinkButton>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<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>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<asp:LinkButton ID="LBNext" runat="server">Next ></asp:LinkButton>

<asp:LinkButton ID="LBLast" runat="server">Last >></asp:LinkButton>

 </div>

and here's the result...







Rate this blog: 
Current rating:
492
     Please login to rate this blog

comments 

Comments:

comments  wow

Posted by: golfman on  5/6/2008 1:55:20 AM


Please login to make a comment