本页面以表格的形式分页显示出所有记录。实现过程如下:  
  1、使用一个包含文件,创建一个数据库连接对象; 
 
  2、创建一个记录集对象; 
 
  3、创建一个表格,第一行用来显示字段名; 
 
  4、判断记录指针是不是到了记录的头部或尾部之外,若是显示提示信息,若不是,则开始进行提取当前页的每一条记录和进行分页; 
 
  5、通过do while 循环语句,将当前页的每一条记录读取出来; 
 
  6、通过for 循环将除当前页码之外的每一个页码做一个超连接; 
 
  7、关闭记录集对象并释放其所占用的所有资源; 
 
  8、关闭连接对象并释放其所占用的所有资源。  
 
<%@language="vbscript"%> 
<!--使用一个包含文件,创建一个数据库连接对象--> 
<!--#include file="connections/conn.asp" --> 
<% 
'创建一个记录集对象。 
set rs_booklist=server.createobject("adodb.recordset") 
sql="select bookid, bookname, bnumber from db_bookinfo order by bnumber desc,bookname" 
rs_booklist.open sql,conn,1,3 
%> 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> 
<html> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=gb2312"> 
<title>图书列表</title> 
<link href="style.css" rel="stylesheet"> 
<style type="text/css"> 
<!-- 
body,td,th { 
font-size: 11pt; 
color: #009999; 
line-height: 1.5; 
} 
body { 
background-image: url(images/bg1.jpg); 
} 
--> 
</style> 
</head> 
<body leftmargin="0" topmargin="0"> 
<div align="center"> 
<!--创建一个表格,第一行用来显示字段名。--> 
<table width="644" border="1" bordercolor="#cccc99" background="images/bg.jpg"> 
<tr> 
<td height="20"><div align="center"><strong>书号</strong></div></td> 
<td height="20"><div align="center"><strong>书名</strong></div></td> 
<td height="20"><div align="center"><strong>数量</strong></div></td> 
<td height="20"> </td> 
</tr> 
<!--*******分页开始******************--> 
<% 
'判断记录指针是不是到了记录的头部或尾部之外,若是显示提示信息,若不是,则开始进行提取当前页的每一条记录和进行分页。 
if rs_booklist.bof and rs_booklist.eof then 
response.write "没有数据" 
else 
'分页显示 
dim page_size '此变量用来存放每一页的记录数。 
dim page_nonce '此变量用来存放当前页的页码。 
dim page_total '此变量用来存放总页数。 
page_size=7 '将第一页记录数设置为7条。 
rs_booklist.pagesize=page_size '将page_size变量中的值赋给rs_booklist记录集对象的页面大小(pagesize)属性。 
page_total=rs_booklist.pagecount '将rs_booklist记录集对象的页面个数(pagecount)属性赋给变量page_total。 
'下面5句,是判断网页是不是刚打开,若是,则将1赋给变量page_nonce(即当前页为第一页), 
'若不是,则将由request对象的querystring集合从http查询字符串中获取来的变量值(当前页码)赋给变量page_nonce。 
if request.querystring("page_nonce")<>"" then 
page_nonce=cint(request.querystring ("page_nonce")) 
else 
page_nonce=1 
end if 
'将当前页码赋给记录集对象的表示当前记录页号的属性(absolutepage)。 
rs_booklist.absolutepage=page_nonce 
dim i 
i=page_size 
'通过do while 循环语句,将当前页的每一条记录读取出来。 
do while not rs_booklist.eof and i>0 
i=i-1 
response.write "<tr align='center'>" 
response.write "<td height='10'>" & rs_booklist("bookid") & "</td>" 
response.write "<td height='10'>" & rs_booklist("bookname") & "</td>" 
response.write "<td height='10'>" & rs_booklist("bnumber") & " </td>" 
%> 
<td width="25" height='10'><div align="center"><a href="buycar_add.asp?bookid=<%= rs_booklist("bookid") %>" target="txtframe"><img src="images/add.gif" alt="添加至购物车" width="18" height="18" border="0" align="middle"></a></div></td> 
<% 
'将记录指针移动到下一条记录。 
rs_booklist.movenext 
loop 
response.write "</table>" 
'开始做分页连接。 
response.write "<p align='center'>分页: " 
'通过for 循环将除当前页码号之外的每一个页码号做一个超连接, 
for j=1 to page_total 
if j=page_nonce then 
response.write j & " " 
else 
response.write "<a href='booklist.asp?page_nonce=" & j & "'>" & j & "</a> " 
end if 
next 
end if 
rs_booklist.close 
set rs_booklist=nothing 
conn.close 
set conn=nothing 
%> 
</table> 
</dir> 
</body> 
</html> |