Article

Home » Server-side Coding » ASP & .NET Tutorials » Paging Results with ASP.NET's PagedDataSource

About the Author

Chris Canal

author_chriscanal Chris works as a Web Developer for a Scottish based web design firm, and is a partner at NMC Group, Inc.. His pride and joy, apart from his fiancee, is his e4ums site, which offers free ASP applications.

View all articles by Chris Canal...

Paging Results with ASP.NET's PagedDataSource

By Chris Canal

November 4th, 2002

Reader Rating: 8.5

Page: 1 2 3 4 5 Next

Paging with ASP.NET? You all know what I'm talking about -- those numbers, or next/previous buttons that Google, Yahoo!, etc. have at the bottom of search results? Well, that's paging. And anyone who's done it with ASP will know how much of a pain in the neck it can actually be.

But with ASP.NET, Microsoft has been nice enough to build the paging functionality into the DataGrid Web Control! Nice of them, eh? With just a few simple properties set to "true", you can easily page any data source... but only if you use a DataGrid!

But what happens if you need to show only a list of links, or some images? The DataGrid carries a large overhead due to the amount of functionality that comes with it. So, to display information, the lightweight Repeater or DataList Web Controls will usually suffice.

"But they lack paging support!" you say. Well... they do and they don't. They lack integrated paging support, but using the PagedDataSource class, we can easily add paging abilities to DataLists and Repeaters, even RadioButton, Checkbox and DropDown lists! PagedDataSource encapsulates all the functionality of DataGrid's paging facility. Let's see how!

Prerequisites

For this article, you should have basic knowledge of ASP.NET and Web Forms.

The PagedDataSource Class

This class comes from the System.Web.UI.WebControls Namespace, so you don't have to import any additional namespaces to the page.

If you've used the paging capabilities of the DataGrid, then you're probably already familiar with all the PagedDataSource properties, and can probably skip this section. If not, here are the properties we'll cover:

  • PageSize - This property takes an integer (by default it's set at 10), and defines how many records are displayed on a page.
  • AllowPaging - This property determines whether the paging should be turned on or off (Boolean "true" or "false"). By default it's set to "false".
  • CurrentPageIndex - This returns/sets the current page number. By default it returns 0.
  • PageCount - Returns a count of the total number of pages that are available.
  • DataSource - The value of this property will be the source of the data you want to page.
  • IsFirstPage - Returns "true" or "false", depending on whether the current page is the first page in the series.
  • IsLastPage - As above, but for the last page.

The data source used has to support indexed access, which means we can't use a DataAdapter (SQL or OLDB). Data sources that we can use include HashTables and DataSets, both of which we'll look at.

Creating an Instance

You can create an instance of the PagedDataSource class in the same way you'd create an instance of anything else:

Dim pagedData As New PagedDataSource()
pagedData.DataSource = mockData
pagedData.AllowPaging = True
pagedData.PageSize = 5

On the first line, we created a new instance of the PageDataSource object, while on the next we set the DataSource. If we left AllowPaging to default, the results wouldn't page, so on the third line we set this property to "true". And on the last line we set the PageSize to 5.

The DataSource

In the example above, we set the DataSource of our instance to mockData. Now, let's create the data source.

For this simple first example, we're going to create a simple HashTable:

Dim mockData As New HashTable()
Dim IDx As Integer = 0
Do Until IDx = 500
  mockData.Add(IDx.toString(), (IDx * 101).toString)
IDx += 1
Loop

On the first line, we create a new instance of the HashTable, and them declare an integer variable. This variable will be used to control the count of the Do Until... Loop statement, and create the data for the table. Lines 3-6 show the Do Until... Loop that creates the data in the HashTable.

The Full Sub-Routine

OK, let's pull all the ASP.NET code together. We'll use it in its own sub-routine, instead of in the Page_Load. Why? For modularity, of course: if it's in its own sub-routine, we'll find it easier to transfer it to another page, put it in a code-behind, or turn it into a Web Control.

Let's take a look at it:

Sub doPaging()
  Dim mockData As New Hash   Dim IDx As Integer = 0
  Do Until IDx = 500
     mockData.Add(IDx.toString(), (IDx * 101).toString)
     IDx += 1
  Loop

  Dim pagedData As New PagedDataSource()
  pagedData.DataSource = mockData
  pagedData.AllowPaging = True
  pagedData.PageSize = 5

  theDataList.DataSource = pagedData
  theDataList.DataBind()
End Sub

Now we've got our basic sub-routine done. We'll be building on this as we go, but that is essentially the nuts and bolts we need.

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

Sponsored Links