電子工業出版社 JSHING HOUSE DF 【例15.8】编程实现如下结果:在浏览器上分页显示Demo数据库 users表中的所有记 录。(基础模块3.1”分页显示”) 分页显示”程序模块是 output pages.asp ouput pages.asp程序流程如下图15.18: 建立一个到数据源的连结 建立记录集,存放查询结果 结果输出(分页显示) 图15.18 output pages,asp程序流程图 从程序流程图中可以看出,程序段“建立一个到数据源的连结”“建立记录集,存放査 询结果”与【例15.4】的模块中这两个程序段相同.只要检查修改相应的参数,就可直接引 用这两个程序段 < output pages.asp源程序〉 licit % 建立一个到数据源的连 Dim strDSN Dim conn demo strDSN="ProviderMSDASQL, DRIVER=ISQL Server SERVER=127.0.0.1: DATABASE=Demo UID=chen, PWD=123 Set connDemo Server CreateObject("ADODB Connection") connDemo. Open strDSN 建立记录集,存放查询结果 Dim rsUsers Dim strSqiselectUsers Set rsUsers= Server CreateObject("ADODB. Recordset") trSqlselectUsers-="SELECT* FROM users" rsUsers. Open strSqISelectUsers, connDemo, 3, 3 分页处理 If rsUsers Record Count=0 Then Msg="无记录 Response. Write("<scriptalert("& errmsg &") history go(-1/script") Response. End
【例 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
電子工業出版社 JEUISHING HOUSE DF ELECTRONCS INDSTR Else Dim totalOfRecords, max Page, which Page, page Action, how Many Records totalofRecords=rsUsers RecordCount rsUsers MoveFirst maxPage=Cint(rsUsers. Page Count) hichPage=Request Querystring("whichPage") pageAction=Request Form("pageAction") Dage="" And pageAc Then whichPage=l End If If pageAction="上一页"Then whichPage=Session("page")-I End If If pageAction="下一页”Then whichPage=Session("page")+I If pageAction="末一页"Then hichPagemaxP End If End if which Pages"" Then End If which Pages1 End If rsUsers. absolutePageswhichPage how Many Recore <!-结果输出(分页显示 head> <metahttp-equiv="content-type"content="Text/html;charset=gb2312"> <tte基础模块“分页显示”< /title>
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>
電子工業出版社 JEUISHING HOUSE DF ELECTRONCS INDSTR <body bgcolor"#COCOC0"> < font faces="华文行楷”sie="6">分页显示</on><h1> If Not rsUsers Eof Then 共有<%= totalofRecords%位用户,<%= rsUsers. PageS ize%条记录顶,目前是 table border"I"cellpadding="8" cellspacing=0" width=342> < th width="70">姓名<t> < th width="121">电话<h> < th widt="143">电子邮箱<h> Do While Not rsUsers Eof And how Many Records<rsUsers PageSize <td width=70"><%=rsUsers("username)%></td> <td width="><=rsUsers("phone")%></td> <td width"143"><%=rsUsers("email")%></td> rsUsers MoveNext owMany Records=howMany Records+1 L Set rsUsers=Nothing </table> <table> Dim pad, scriptName, counter, ref
</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
電子工業出版社 JEUISHING HOUSE DF ELECTRONCS INDSTR ariables("script name") f counter=lo Then End If ref="<a href="& scriptName whichPage="& counter ref=ref &"> pad counter &"</a> f counter Mod 10=0 Then Next </td> <form method="post" action="output pagers. asp"> If whichPage>I Then 第一页 nput name=" pageAction"type=" submit" value="上一页"> If cint( which Page)<cint( maxPage) Then < Input name=" page Action"type=" submit" value="下一页"> < Input name=" pageAction"type=" submit" value="末一页"> End If End If </td> </u>
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>
電子工業出版社 JEUISHING HOUSE DF ELECTRONCS INDSTR <htable> </form> ybody? </html>
</table> </form> </body> </html>