Article

Paging Results with ASP.NET's PagedDataSource

Page: 1 2 3 4 5 Next

The Actual Paging

As you can see, we're sending 5 items to the browser. Fantastic... but how do we page? Well, let's look at it.

First, we'll create "next" and "previous" buttons. So, below or above the DataList, add the following:

<asp:LinkButton id="btnPrev" Text="&lt;" OnClick="Prev_Click"  
runat="server" />  
<asp:LinkButton id="btnNext" Text="&gt;" OnClick="Next_Click"  
runat="server" />

This will create < and > wherever you've placed the code. If you save the page and refresh it, an error will be displayed saying 'Prev_Click' is not a member of 'ASP.pageName_aspx'". That's ok, we'll fix this error shortly.

Get Paging

So far, we're returning 5 records to the page and next/previous buttons, so it looks like it's paging, but we still can't actually page. Well, my impatient friend, we will soon! First off, let's create the two sub-routines that are called by the paging buttons, Prev_Click and Next_Click. Just below our doPaging() sub-routine, add the following code:

Public Sub Prev_Click(ByVal obj As Object, ByVal e As EventArgs)  
  Response.Redirect(Request.CurrentExecutionFilePath    
  & "?Page=" & (pagedData.CurrentPageIndex - 1))  
End Sub  
 
Public Sub Next_Click(ByVal obj As Object, ByVal e As EventArgs)  
  Response.Redirect(Request.CurrentExecutionFilePath    
  & "?Page=" & (pagedData.CurrentPageIndex + 1))  
End Sub

If you add this code and save it, the page will actually return an error, saying: "Name 'pagedData' is not declared". This error is returned because the object is only declared inside the doPaging() sub-routine, and we're trying to access a property of it in each of the two new functions. To fix this error, what we have to do is make the PagedDataSource object available tall the code in the page. To accomplish this, we simply remove this line from inside doPaging() and place it just above all the sub-routines:

Dim pagedData As New PagedDataSource()

With this done, our code now looks like this:

Dim pagedData As New PagedDataSource()  
Sub Page_Load(byVal obj As Object, byVal e As EventArgs)  
  doPaging()  
End Sub  
 
Sub doPaging()  
  Dim mockData As New HashTable()  
  Dim IDx As Integer = 0  
  Do Until IDx = 500  
     mockData.Add(IDx.toString(), (IDx * 101).toString)  
     IDx += 1  
  Loop  
 
  pagedData.DataSource = mockData  
  pagedData.AllowPaging = True  
  pagedData.PageSize = 5  
 
  theDataList.DataSource = pagedData  
  theDataList.DataBind()  
End Sub  
 
Public Sub Prev_Click(ByVal obj As Object, ByVal e As EventArgs)  
  Response.Redirect(Request.CurrentExecutionFilePath  
  & "?Page=" & (pagedData.CurrentPageIndex - 1))  
End Sub  
 
Public Sub Next_Click(ByVal obj As Object, ByVal e As EventArgs)  
  Response.Redirect(Request.CurrentExecutionFilePath  
  & "?Page=" & (pagedData.CurrentPageIndex + 1))  
End Sub

The two new functions access the CurrentPageIndex of the PagedDataSource object, which returns the index of the current page. The Next_Click function adds 1 to it, and then redirects your browser with the value appended to the URL as a Querystring. The Prev_Click does the same, but takes 1 away.

But... nothing seems to be happening, right? Ok, let's remedy that. Below you will find a revised version of the doPaging() sub-routine. The new lines are in bold:

Sub doPaging()  
  Dim mockData As New HashTable()  
  Dim IDx As Integer = 0  
  Do Until IDx = 500  
     mockData.Add(IDx.toString(), (IDx * 101).toString)  
     IDx += 1  
  Loop  
 
  pagedData.DataSource = mockData  
  pagedData.AllowPaging = True  
  pagedData.PageSize = 5  
 
  Try  
     pagedData.CurrentPageIndex =  
Int32.Parse(Request.QueryString("Page")).ToString()  
  Catch ex As Exception  
     pagedData.CurrentPageIndex = 0  
  End Try  
 
  btnPrev.Visible = ( NOT pagedData.IsFirstPage )  
  btnNext.Visible = ( NOT pagedData.IsLastPage )  
 
  theDataList.DataSource = pagedData  
  theDataList.DataBind()  
End Sub

Try it, and watch it page!

We use the Try... Catch statement to parse out the page number supplied by the QueryString, and change the PagedDataSource object's CurrentPageIndex. If it isn't a number, the Catch block will run and set it to 0 -- making this the first page!

The two lines below simply make the Next/Previous buttons disappear at the appropriate time!

btnPrev.Visible = ( NOT pagedData.IsFirstPage )  
btnNext.Visible = ( NOT pagedData.IsLastPage )

This code works by accessing the IsFirstPage and IsLastPage properties of the PagedDataSource object, which returns a Boolean.

It couldn't be simpler! Below you can find a screenshot of what the code produces:

921_Result2

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links