站长中国
设为首页 | 站长论坛

站长论坛 站长下载
您所在的位置: 站长中国 > 站长学院 > 网络编程 > NET专区 > 正文

关于数据分页(转自www.codeproject.com)
  2007年09月14日19:06:59  评论(1条) 字体:[ ]
相关热点: 关于 the of to we is page that and in Keyword have
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 you will receive an error.

Let's try to open a database connection and get the data:

    Try
        odConn = New OleDbConnection(strConn)
        odAdapt = New OleDbDataAdapter(SQL, odConn)
        DS = New DataSet
        odAdapt.Fill(DS)
        
        ' Get our DataTable
        DT = DS.Tables(0)

        ' Get record count
        nRecCount = DT.Rows.Count

    Catch e As Exception
        Response.Write("Error: <b>" & e.Message & "</b><p>")
        nRecCount = 0
    End Try

This is what’s going on in the above lines of code: First we construct new OleDbConnection object using our connection string. We create OleDbDataAdapter next providing our SQL statement and reference to the connection object. After creating new DataSet object we instruct our DataAdapter to populate (fill) DataSet with the data out of our database. Then we get a reference to the first table in the DataSet that represents our data and retrieve the number of records (rows) returned in that table. Try and Catch are obviously there to try and catch any errors during these database operations.

4. Navigation Links
Yes it is a fourth step. I did leave the third step (displaying of the results) for the last because in order for us to display the records we need to figure some things out. I also think it is better to create and display navigation links on the top of the page before the results.

At this point we have to figure out couple of things: do we have any results from our search and if so how many pages of results do we have?

Presence of the results is easily determent by checking record count (notice we do not have EOF property anymore):


If nRecCount = 0 Then
    ... ' Clean up
    ... ' Do the no results HTML here
    Response.Write("No Items found.")
    Response.End
    ... ' Done
End If

The number of pages we have is obviously depends on the number of items we want to display per page. ADO.NET does not have all those cool properties that we’ve come to like and use in ADO. PageSize, PageCount, AbsolutePage properties of ADO Recordset object are not available to us anymore. We will have to resort to some very simple calculations in order to determent number of pages of data we have at hand.

nPageCount = nRecCount \ RECORDS_PER_PAGE
If nRecCount Mod RECORDS_PER_PAGE > 0 Then
    nPageCount += 1
End If

Now we need to talk about the current page number. Since we want this page (results.aspx) to be able to display any one of the pages of results we have to have a way to specify which page will the user see right now. We will do it by passing an additional parameter to our results.aspx script that we will call "Page". So the link to our page could look like this:

http://..../results.aspx?Keyword=BlahBlahBlah&Page=3

With that said we can figure out the number of the page that is requested by the user by simple checking Page parameter:

nPage = Convert.ToInt32(Request.QueryString("Page"))

We need to make sure that the number of page requested by user is within the acceptable range and fix it if needed:

If nPage < 1 Or nPage > nPageCount Then
    nPage = 1
End If

Now we can create our navigation links by simply linking to this page with the same value for Keyword and the specific page number. For example:

' First page
Response.Write("<A HREF=""results.aspx?Keyword=" & Keyword & "&Page=1"">First Page</A>")
' Previous page
Response.Write("<A HREF=""results.aspx?Keyword=" & Keyword & "&Page=" & (nPage - 1).ToString() & """>Prev. Page</A>")
' Next page
Response.Write("<A HREF=""results.aspx?Keyword=" & Keyword & "&Page=" & (nPage + 1).ToString() & """>Next Page</A>")
' Last page
Response.Write("<A HREF=""results.aspx?Keyword=" & Keyword & "&Page=" & nPageCount.ToString() & """>Last Page</A>"
' 15th page
Response.Write("<A HREF=""results.aspx?Keyword=" & Keyword & "&Page=15"">15th Page</A>")

There is an alternative way of providing navigational links provided in my sample script.

3. Display a page of results
All we have left to do is to display a page of results.

Before we do that though we need to find out what are the indexes of the starting and ending rows (records) of this page are:

nStart = RECORDS_PER_PAGE * (nPage - 1)
nEnd = nStart + RECORDS_PER_PAGE - 1
If nEnd > nRecCount - 1 Then
    nEnd = nRecCount - 1
End If

Now we are ready to output a page of records. In contrast to ADO, with ADO.NET we should not use While loop. We already know the indexes of records we want to show, so For loop will work just great:

For i = nStart To nEnd
    Response.Write(DT.Rows(i)("ItemName") & "<br>")
Next

That is it. Sample that I have included here is a bit more complex, because I've combined both HTML search form and displaying of the results in one page. That is why there is an additional Mode parameter being used in every link. All the code concerning displaying of the results of the search is in the ShowResults() function.

  



责任编辑:

收藏本文 打印 打印本文  推荐本文 告诉好友 投稿 投稿邮箱

站长排行

学院

新闻

专栏

盈利

[揭密网络黄链]中国留学生买凶专破日本
JSP语法(6)
超强弹出窗口代码,什么都挡不住
FLASH视觉特效实例之地震效果
贴吧发帖机使用教程(绝对原创)
关于数据分页(转自www.codeproject.co
ASP实现文件直接下载
Photoshop制作光感超酷效果水晶球
 遍历ASP.NET页面控件
永远的后门[经典]+查不出的后门
淘宝网卖家公然叫卖“艳照门”照片集
驳《百度Hi面世对腾讯有利》
Google绿色专家质疑黑色背景网页节省资
国内各IT企业办公环境揭秘(多图)
阿里妈妈广告卖主全攻略
站长创业源动力 主流站长站赏析
推荐阅读:80年小子的创业道理
Discuz!6.0猛将出击 最强论坛程序酷炫
我的网络,我的团队:专访李文明
百度新闻频道改版十天 流量止跌反弹翻
ECSHOP模板制作参考文档
悬挂阿里妈妈会否被百度惩罚
阿里妈妈是否是中小站长的救世主?
最强网店ECShop发新版 众多酷炫功能给
ECSHOP模板下载
土豆网,优酷网,爆米花等视频网站采集
DedeCms模板安装/制作概述
网上商店系统巅峰对决 ECShop vs ShopE
艰难的走在创业的路上 第一天
编程中国全站采集规则
性福联盟 一个不尊重站长的联盟
大脚:日赚100元—揭露最新firefox欺骗
大脚:垃圾站超级赚钱法之二—突破“站
大脚:垃圾站超级赚钱法之——前言
迅雷联盟、快车联盟收入对比
经理人必看的十个管理网站
Google Adsense的秘密 第二版
西联汇款兑付城市查询
不用SEO取得成功的10个步骤
关于做GOOGLE的五条经验
站长学院  网页设计 建站教程 图形图象 网络编程

Photoshop CS3
Photoshop CS3
不用Photoshop
不用Photoshop

DIV+CSS的开发方式 听听另外的
虚拟主机建站动易里快速生成的
VBScript特效代码 满屏幕乱跑
牛气!一个菜鸟站长的超强网站
创建、维护一个个人博客的“投
让网站流量稳步飙升的秘籍
网站推广的基本思想

新闻线索

如果你有站长界人事变动、重组并购、变革技术出现,以及产品投诉等重要新闻线索,请告诉我们,我们会给予特别关注。
0631-3653338
站长中国编辑部
站长中国24小时新闻热线: 13256307008