【例15.8】编程实现如下结果:在浏览器上分页显示Demo数据库users表中的所有记录。(基础模块3.1”分页显示”)
“分页显示”程序模块是output_pages.asp
ouput_pages.asp程序流程如下图15.18:

图15.18 output_pages.asp程序流程图从程序流程图中可以看出,程序段“建立一个到数据源的连结”“建立记录集,存放查询结果”与【例15.4】的模块中这两个程序段相同.只要检查修改相应的参数,就可直接引用这两个程序段。
<output_pages.asp源程序>:
<% Option Explicit %>
<%
'建立一个到数据源的连接
Dim strDSN
Dim connDemo
strDSN="Provider=MSDASQL;DRIVER={SQL Server};SERVER=127.0.0.1;DATABASE=Demo;UID=chen;PWD=123"
Set connDemo = Server.CreateObject("ADODB.Connection")
connDemo.Open strDSN
'建立记录集,存放查询结果
Dim rsUsers
Dim strSqlSelectUsers
Set rsUsers = Server.CreateObject("ADODB.Recordset")
strSqlSelectUsers="SELECT * FROM users"
rsUsers.Open strSqlSelectUsers,connDemo,3,3
'分页处理
If rsUsers.RecordCount=0 Then
Dim errMsg
errMsg="无记录"
Response.Write("<script>alert('" & errmsg & "');history.go(-1)</script>")
Response.End

Else
Dim totalOfRecords,maxPage,whichPage,pageAction,howManyRecords

totalOfRecords=rsUsers.RecordCount
rsUsers.MoveFirst
rsUsers.PageSize=12
maxPage=Cint(rsUsers.PageCount)
whichPage=Request.Querystring("whichPage")
pageAction=Request.Form("pageAction")

If whichPage="" And PageAction<>"" Then
If pageAction="第一页" Then
whichPage=1
End If
If pageAction="上一页" Then
whichPage=Session("page")-1
End If
If pageAction="下一页" Then
whichPage=Session("page")+1
End If
If pageAction="末一页" Then
whichPage=maxPage
End If
End if
If whichPage="" Then
whichPage=Session("page")
End If
If whichPage="" Then
whichPage=1
End If
rsUsers.AbsolutePage=whichPage
Session("page")=whichPage
howManyRecords=0
%>
<!--结果输出(分页显示)-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>基础模块“分页显示”</title>
</head>
<body bgcolor="#C0C0C0">
<p>
<font face="华文行楷" size="6">分页显示</font></h1>
</p>

<%
If Not rsUsers.Eof Then
%>
<p>
共有<% =totalOfRecords %>位用户,<% =rsUsers.PageSize %>条记录/页,目前是第<% =whichPage %>页
</p>
<table border="1" cellpadding="8" cellspacing="0" width="342">
<tr>
<th width="70" >姓名</th>
<th width="121" >电话</th>
<th width="143" >电子邮箱</th>
</tr>
<%
Do While Not rsUsers.Eof And howManyRecords<rsUsers.PageSize
%>
<tr>
<td width="70"><% =rsUsers("username") %></td>
<td width="121"><% =rsUsers("phone") %></td>
<td width="143"><% =rsUsers("email") %></td>
<%
rsUsers.MoveNext
howManyRecords=howManyRecords+1
Loop
rsUsers.Close
Set rsUsers=Nothing
%>

</table>
<table>
<tr>
<td>
<%
Dim pad,scriptName,counter,ref

pad="0"
scriptName=Request.ServerVariables("script_name")
For counter=1 To maxPage
If counter>=10 Then
pad=""
End If

ref="<a href=" & scriptName & "?whichPage=" & counter &""
ref=ref & ">" & pad & counter & "</a>"
Response.Write ref & " "

If counter Mod 10 = 0 Then
Response.Write "<br>"
End If
Next
%>
</td>
</tr>
</table>

<form method="post" action="output_pagers.asp">
<table>
<tr>
<td>
<%
If whichPage>1 Then
%>
<input name="pageAction" type="submit" value="第一页">
<input name="pageAction" type="submit" value="上一页">
<%
End If

If cint(whichPage) <> cint(maxPage) Then
%>
<input name="pageAction" type="submit" value="下一页">
<input name="pageAction" type="submit" value="末一页">
<%
End If
End If
End If
%>
</td>
</tr>
</table>
</form>
</body>
</html>