日期:2012-06-18  浏览次数:20431 次

First of all, let me say a few words about this article. This is not an entirely original article but an ASP.NET adaptation of the ASP article that I’ve posted a few months ago. Original article can be found here: ADO Recordset Paging in ASP.

The purpose of this article is to show how to implement data paging with ADO.NET. It is not a generic ASP.NET or even ADO.NET tutorial. The article looks at the very specific case when you need a better control over your own custom presentation of data and of navigation provided to your users. This is why you will not see any WebForms or WebControls here. I did not use new DataGrid control even though it comes with the data paging capabilities. DataGrid control is very powerful and has a lot of useful features, but it is somewhat limiting in the way of presentation of data and page navigation links. Beside that, there are plenty of samples and articles on how to use new ASP.NET capabilities written by other people.

Every once in a while I come across the task of displaying a large number of records on the web. The good example is displaying the results of a search. Most of the time I do not know the number of records that I have to display in advance. In addition to this, as the usage of the application growth the size of the database will grow accordingly. That leaves me as well as anyone with the similar application requirements no other choice, but to develop some kind of algorithm to display records in the smaller chunks - pages.


Everyone is familiar with the way search results are displayed by Internet search engines. You get the first page of results that are limited to some number of records (20 for example) and some navigational links to go to the first, previous, next or the last page. Some sites give you the ability to go directly to specific page number, some use a mixture of both.

So how does one implements data paging mechanism with ASP.NET? Specifically, how do we implement record paging using ADO.NET?

Let’s pretend that we have a database with the table called tblItem that is used to store information about our Items (whatever they are?). Let me also imagine that one of the fields in tblItem called ItemName. We are given a task of creating a set of pages to give a user an ability to search for the items by the ItemName field. We decided to make a set of two pages. One page will display the search form and one for the results of the search.

Please excuse me, but I will skip all the variable declarations and HTML formatting.

First page should be easy. It’s a standard HTML form that could look something similar to this:

...
<FORM ACTION="results.asp" METHOD="GET">
Item Name: <INPUT TYPE="text" NAME="Keyword"> <INPUT TYPE="submit" VALUE=" Find ">
</FORM>
...

Second page is where all the magic should happen. This is what the second page (results.aspx) should be able to do:
1. Receive the Keyword that user have entered.
2. Search the database for records containing Keyword.
3. Display a page of resulting records.
4. Provide user with some navigation links to display more pages of results if needed.


1. Receive Keyword
Receiving the Keyword is as easy as:

Keyword = Request.QueryString("Keyword").Trim()

2. Search the database and retrieve data.
Now we have everything we need to get an ADO.NET DataSet with the items that contain our keyword in their ItemName.

First we create a sql statement that will do the search:

SQL = "SELECT * FROM tblItem WHERE ItemName LIKE '%" & Keyword.Replace("'", "''") & "%'"

Notice that I’ve used Replace function to double single quotes in the search string. Without it if user enters a single quote in his/her Keyword