主要学习了如何对大量的数据进行分页显示,编写了index.asp,使用了conn.asp和css.asp(源码参见以前的文章)。SQL版和RecordSet版的区别在于,SQL在大数据量的查询中速度要快的多。
演示地址:http://door.it50.net/page2/index.asp
代码下载:http://door.it50.net/index2.html
index.asp
<!--#i nclude file="conn.asp"-->
<!--#i nclude file="css.asp"-->
</head>
<table width="600" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><div align="center">ID</div></td>
<td><div align="center">标题</div></td>
<td><div align="center">作者</div></td>
<td><div align="center">正文</div></td>
<td><div align="center">时间</div></td>
</tr>
<%
dim allcounts,counts,allpage,page,url,exec,jishu,sql
'allcounts是记录总数
'counts是每页显示的记录数
'allpage是总的页码
'page是sql中top xx的值
'url是显示记录的页面文件名
'jishu是页码
url="index.asp"
exec="select * from lyb"
set rs=server.createobject("adodb.recordset")
rs.open exec,conn,1,1
allcounts=rs.recordcount
counts=30
if cint(allcounts/counts)<(allounts/counts) then
allpage=(cint(allcounts/counts)+1)
'如果四舍五入后的页码小于原页码,则总页数等于四舍五入后的页码+1
else
allpage=cint(allcounts/counts)
end if
rs.close
set rs=nothing
'以上是计算出总页码的值
jishu=request("jishu")
if jishu="" or jishu&"a"="a" or (not isnumeric(jishu)) then
'如果页码为空/为刚打开页面时的状态/不为数字
jishu=1
else
jishu=cint(jishu)
end if
'超级弯路:asp是弱类型语言,在进行数值计算和数值比较时,记得用 CInt()
if jishu>allpage then
jishu=cint(allpage)
end if
'如果页码数大于总页数,则使二者相等
page=counts*jishu
sql="select top "&counts&" b.* from (select top "&page&" id,id from lyb order by id desc) a,lyb b where b.id = a.id order by a.id"
set rs1=server.createobject("adodb.recordset")
rs1.open sql,conn,1,1
rs1.MoveLast
do while (not rs1.eof)
'如果数据库中有记录且设定的每页显示数量大于0
%>
<tr>
<td width="50"><div align="center"><%=rs1("ID")%></div></td>
<td width="50"><div align="center"><%=rs1("bt")%></div></td>
<td width="50"><div align="center"><%=rs1("zz")%></div></td>
<td width="50"><div align="center"><%=rs1("zw")%></div></td>
<td width="180"><div align="center"><%=rs1("times")%></div></td>
</tr>
<%
rs1.MovePrevious
if rs1.bof then exit do
loop
%>
</table>
<table align="center">
<tr>
<td width="600" align=right valign="bottom" class="STYLE3" headers="50"><table width="600" height="56" border="0" align="center" cellspacing="3" class="STYLE3">
<tr>
<td height="46" align="right" valign="top">
<%
'显示总条数
response.Write("<br/>共有 "&allcounts&"条记录,")
if jishu=1 then
response.Write("首页 上一页 ")
else
response.Write "<a href="&url&"?jishu=1>首页</a> <a href="&url&"?jishu="&jishu-1&">上一页</a> "
end if
'页码为1时首页/上一页不加链接,否则加链接
if jishu=allpage then
response.Write "下一页 末页"
else
response.Write "<a href="&url&"?jishu="&jishu+1&">下一页</a> <a href="&url&"?jishu="&allpage&">末页</a> "
end if
response.Write ",当前位于第"&jishu&"页,共"&allpage&"页"
%>
</td>
</tr>
</table></td>