在了解了Recordset对象的以上属性和方法后,我们来考虑一下,如何运用它们来达到我们分页显示的目的。首先,我们可以为PageSize属性设置一个值,从而指定从记录组中取出的构成一个页的行数;然后通过RecordCount属性来确定记录的总数;再用记录总数除以PageSize就可得到所显示的页面总数;最后通过AbsolutePage属性就能完成对指定页的访问。好象很并不复杂呀,下面让我们来看看程序该如何实现呢?
子的自动编号;“s我们建立这样一个简单的BBS应用程序,它的数据库中分别有以下五个字段:“ID”,每个帖ubject”,每个帖子的主题;“name”,加帖用户的姓名;“email”,用户的电子邮件地址;“postdate”,加帖的时间。数据库的DSN为“bbs”。我们将显示帖子分页的所有步骤放在一个名为“ShowList()”的过程中,方便调用。程序如下:
'----BBS显示帖子分页----
1 Sub ShowList()
1
2PgSz=20 '设定开关,指定每一页所显示的帖子数目,默认为20帖一页
3Set Conn = Server.CreateObject("ADODB.Connection")
4Set RS = Server.CreateObject("ADODB.RecordSet")
5sql = "SELECT * FROM message order by ID DESC"
6'查询所有帖子,并按帖子的ID倒序排列
7Conn.Open "bbs"
8RS.open sql,Conn,1,1
9If RS.RecordCount=0 then
10response.write "
<p><center>对不起,数据库中没有相关信息!</center></p>
1"
2else
3RS.PageSize = Cint(PgSz) '设定PageSize属性的值
4Total=INT(RS.recordcount / PgSz * -1)*-1 '计算可显示页面的总数
5PageNo=Request("pageno")
6if PageNo="" Then
7PageNo = 1
8else
9PageNo=PageNo+1
10PageNo=PageNo-1
11end if
12ScrollAction = Request("ScrollAction")
13if ScrollAction = " 上一页 " Then
14PageNo=PageNo-1
15end if
16if ScrollAction = " 下一页 " Then
17PageNo=PageNo+1
18end if
19if PageNo <1 Then
20PageNo = 1
21end if
22n=1
23RS.AbsolutePage = PageNo
24Response.Write "
<center>"
position=RS.PageSize*PageNo
pagebegin=position-RS.PageSize+1
if position <rs.recordcount "<p="" else="" end="" if="" pagend="RS.RecordCount" response.write="" then=""><font color="Navy"><b>数 据 库 查 询 结 果:</b>"
Response.Write "(共有"&RS.RecordCount &"条符合条件的信息,显示"&pagebegin&"-"&pagend&")</font>"
Response.Write "<table bgcolor="#FFFFFF" border="1" cellpadding="4" cellspacing="0" width="600">"
Response.Write "<tr bgcolor="#5FB5E2"><font size="2"><td><b>主 题</b></td><td><b>用 户</b></td><td><b>Email</b></td><td><b>发 布 日 期</b></td></font><tr bgcolor="#FFFFFF">"
Do while not (RS is nothing)
RowCount = RS.PageSize
Do While Not RS.EOF and rowcount > 0
If n=1 then
Response.Write "<tr bgcolor="#FFFFFF">"
ELSE
Response.Write "<tr bgcolor="#EEEEEE">"
End If
n=1-n
1<td><span style="font-size:9pt"><a href='view.asp?key=```
2 =RS("ID")
3```'>```
4 =RS("subject")
5```</a></span></td>
6<td><span style="font-size:9pt">```
7 =RS("name")
8```</span></td>
9<td><span style="font-size:9pt"><a ```"="" email")="" href="mailto:```
10 =RS(">```
11 =RS("email")
12```</a></span> </td>
13<td><span style="font-size:9pt">```
14 =RS("postdate")
15```</span> </td>
16</tr>
RowCount = RowCount - 1
RS.MoveNext
Loop
set RS = RS.NextRecordSet
Loop
Conn.Close
set rs = nothing
set Conn = nothing
1</tr></tr></tr></table>
2<form action="list.asp" method="GET">
3<input name="pageno" type="HIDDEN" value="```
4 =PageNo
5```"/>
if PageNo > 1 Then
response.write "<input name="ScrollAction" type="SUBMIT" value=" 上一页 "/>"
end if
if RowCount = 0 and PageNo <>Total then
response.write "<input name="ScrollAction" type="SUBMIT" value=" 下一页 "/>"
end if
response.write "</form>"
End if
End Sub