≮海浪轻风≯

笑对人生,珍惜所有!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

asp数据分页显示Recordset版|SQL版

Posted on 2007-05-09 10:20  ≮海浪轻风≯  阅读(179)  评论(0)    收藏  举报
asp之数据的分页显示RecordSet版
 

主要学习了如何对大量的数据进行分页显示,编写了index.asp,使用了conn.asp和css.asp(源码参见以前的文章)。

演示地址:http://door.it50.net/page/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" class="STYLE3">
  <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 counts,allpage,page,url,exec
'定义变量
'counts是每页显示的记录数
'allpage是总的页码
'page是当前页码
'url是显示记录的页面文件名
url="index.asp"

exec="select * from lyb order by times desc,id desc"
//将查询语句赋予变量exec
set rs=server.createobject("adodb.recordset")
//定义记录集组件,所有搜索到的记录都放在这里,rs只是随意定义的变量名
rs.open exec,conn,1,1


if rs.eof and rs.bof then
response.Write("no find")
'如果发现数据指针已经到了记录头和记录尾,显示“no find”,即未发现记录
else
    counts=30
 '每页显示30条记录
 rs.pagesize=counts
 '设定RecordSet对象的pagesize属性,指定一个逻辑页中的记录个数
 allpages=rs.pagecount
 'rs.pagesize属性设定后rs.pagecount自动计算一共有多少页
 page=request("page")
 '获取当前页码
 if not isnumeric(page) then
  page=1
  end if
  '如果页码数不全为数字,则设定页码为1
 if isEmpty(page) or cint(page)<1 then
  page=1
  '如果page变量未被初始化或page变量四舍五入后小于1,则设page的值为1(预防走到头)
 elseif cint(page)>=allpages then
  page=allpages
 end if
 '否则,如果page变量四舍五入后大于等于总页码,则设page变量的值为总页码(预防走到尾)
 rs.absolutePage=page
 '设定RecordSet对象的当前页
do while (not rs.eof) and counts>0
'如果数据库中有记录且设定的每页显示数量大于0
%>
  <tr>
    <td width="50"><div align="center"><%=rs("ID")%></div></td>
    <td width="50"><div align="center"><%=rs("bt")%></div></td>
    <td width="50"><div align="center"><%=rs("zz")%></div></td>
    <td width="50"><div align="center"><%=rs("zw")%></div></td>
    <td width="180"><div align="center"><%=rs("times")%></div></td>
  </tr>
  <%
  counts=counts-1
  rs.movenext
//下移一条记录
  if rs.eof then  exit do 
    loop
//返回循环首
end if
%>
</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>共有&nbsp;"&rs.recordcount&"条记录,")

if page=1 then
 response.Write("首页&nbsp;上一页&nbsp;")
else
response.Write "<a href="/index.asp?page=1>";首页</a>&nbsp;<a href="/index.asp?page=""&page-1&">上一页</a>&nbsp;"
end if
'页码为1时首页/上一页不加链接,否则加链接

if page=allpages then
 response.Write "下一页 末页"
else
response.Write "<a href="/index.asp?page=""&page+1&">下一页</a>&nbsp;<a href="/index.asp?page=""&allpages&">末页</a>&nbsp;"
end if
response.Write ",当前位于第"&page&"页,共"&allpages&"页"
%>
</td>
    <td width="100" align="center" valign="bottom">
<%
allwrite="到第<input type='text' style='width:30px;' Name='page'>页<input type='submit' value="/GO'>"
 Response.Write("<form name='formPage' method='post' action="&URL&"><align=right>" & allWrite & "</form>")
 '实现到第几页去
 '上一方式实现起来将会自动换到第2行,无法在前一个response.write后面显示,暂时无法解决。
%>
</td>
  </tr>
</table></td>

asp之数据的分页显示SQL版
 

主要学习了如何对大量的数据进行分页显示,编写了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/>共有&nbsp;"&allcounts&"条记录,")

if jishu=1 then
response.Write("首页&nbsp;上一页&nbsp;")
else
response.Write "<a href="&url&"?jishu=1>首页</a>&nbsp;<a href="&url&"?jishu="&jishu-1&">上一页</a>&nbsp;"
end if
'页码为1时首页/上一页不加链接,否则加链接

if jishu=allpage then
response.Write "下一页 末页"
else
response.Write "<a href="&url&"?jishu="&jishu+1&">下一页</a>&nbsp;<a href="&url&"?jishu="&allpage&">末页</a>&nbsp;"
end if
response.Write ",当前位于第"&jishu&"页,共"&allpage&"页"
%>
</td>

</tr>
</table></td>