当前位置: 移动技术网 > IT编程>开发语言>Asp > asp中"无限流"分页程序代码

asp中"无限流"分页程序代码

2017年12月12日  | 移动技术网IT编程  | 我要评论
<% '****************************************************************** '** 本程序
<%
'******************************************************************
'** 本程序名:"无限流"分页程序
'** 作者:arbiter(aasx)
'** 版本:million level
'**
'** qq:22222xx
'** email:arbiter@21cn.com
'** http://www.imagecity.org/
'******************************************************************
'**
'** 【作者的话】
'**
'** 分页程序无疑是许多网络程序功能中一个比较麻烦的东西,事实上现在
'** 为止绝大部分人还是在使用传统的分页方法(rs.pagesize=xx),而了解
'** 数据库操作的人都知道,这种传统方式有个弊端:第一次打开页面时,
'** 它会预读所有的记录集,这当在数据大的时候,这将是致命的,而且接
'** 下来的翻页速度也会非常慢,很占用资源。对于十万数量级以上的数据
'** 库这种传统分页方式已经显得非常无力,更别说百万级了(根本没法操
'** 作)。基于这种原因,促使我做了本程序。
'**
'** 【程序功能】
'**
'** 针对大型的数据库进行分页操作,理想的可操作的数据记录量在200万
'** 以内(max level版将无数量限制,且无论数据库多大,翻页速度都是
'** 不变),这是million level版分页程序在赛扬1g、内存512、win2k环
'** 境下的测试数据:
'**
'** sqlserver 2k + 10万条记录 + 每页显示20条:
'** 平均翻页速度:45ms
'** sqlserver 2k + 100万条记录 + 每页显示20条:
'** 平均翻页速度:350ms
'**
'**
'** 【分页原理】
'**
'** 本程序不再使用rs.pagesize的方式分页,连接数据库的游标类型
'** 也不是使用conn,1,x,而是conn,0,1,这应是最快的游标类型了,不要
'** 以为这样会使程序变得复杂,相反,程序非常简单,如果你看不明白,
'** 应该是我的编程风格你不习惯,而非程序复杂。
'** "无限流"分页的中心是:每页只读出需要显示的记录,不再象传统
'** 分页程序预读全部的数据,这正在本程序最大的优点--占用资源少,同
'** 理速度也得到非常大的提升,特别在数据量越大的时候,它的速度优势
'** 越明显(100万记录才350ms左右)。
'** 当程序执行后,使用curcorbegin和curcorend记录显示的第一条记
'** 录和最后一条记录的id值,作为下一次翻页的标记,然后利用top xx取
'** 出需要的数据显示,同时又再对id值进行记录。
'**
'** 【结 言】
'**
'** 本程序为共享版,提供给各程序爱好者研究使用,若要转载、散播、修
'** 改或作其他用途,请尊重作者的辛劳,注明出处。
'** 如果本程序中有错漏、非最优化等缺点,请到www.csdn.net的web开发/
'** asp栏目中发表讨论,为了中国软件事业的发展,请不要固步自封:)
'**
'********************************************************************  
option explicit
'response.flush
dim begintime,endtime
begintime=timer
dim conn,sqlstr,rs,defrecordnum,cursorbegin,cursorend,curpagenum,hav
defrecordnum=20
'--------------获取相关参数----------
if request("cursorbegin")="" then cursorbegin=0 else cursorbegin=request("cursorbegin")
if request("cursorend")="" then cursorend=0 else cursorend=request("cursorend")
if request("curpagenum")<>"" then
curpagenum=clng(request("curpagenum"))
if curpagenum<=0 then curpagenum=1
else
curpagenum=1
end if
hav=request("hav")
if hav="" then hav="next"
'----------------end-----------------
'------------显示翻页内容函数--------
function turnpagefs(disprecordnum)
dim n
while not(rs.eof) and n<disprecordnum
n=n+1
response.write "<tr>"&_
"<td bgcolor='efefef'>"&rs(0)&"</td>"&_
"<td bgcolor='efefef'>"&rs(1)&"</td>"&_
"<td bgcolor='efefef'>"&rs(2)&"</td>"&_
"<td bgcolor='efefef'>"&rs(3)&"</td>"&_
"<td bgcolor='efefef'>"&rs(4)&"</td>"&_
"<td bgcolor='efefef'>"&rs(5)&"</td>"&_
"</tr>"
if n=1 then cursorbegin=rs(0)
if n=defrecordnum or rs.eof then cursorend=rs(0)
rs.movenext
wend
end function
'-------------连接数据库-------------
set conn=server.createobject("adodb.connection")
'sqlstr="provider=microsoft.jet.oledb.4.0;data source="&server.mappath("mldata.mdb")
sqlstr="driver={sql server};server=arbiter;uid=arbiter;pwd=123456;database=mldata"
conn.open sqlstr
'---------统计总记录数/总页数---------
'-ps:推荐使用count(id),id为自动编号且索引,否则速度有可能大打折扣
'-ps:此统计是本程序中最耗资源的一部分,如果取消这段程序,速度会快上10倍左右
dim totalrecords,totalpages
sqlstr="select count(id) as recordsum from abc"
set rs=conn.execute(sqlstr,0,1)
totalrecords=rs("recordsum")
totalpages=abs(int(totalrecords/defrecordnum*(-1)))
rs.close
set rs=nothing
'--------根据hav选择相应的sql字串-----
select case(hav)
case "back"
cursorend=cursorbegin
sqlstr="select top "&defrecordnum&"_
id,title,filename,k,imgsize,nameson _
from abc where id<"&cursorbegin&_
" and id in (select top "&defrecordnum_
&" id from abc where id<"&cursorbegin_
&" order by id desc) order by id"
case "next"
sqlstr="select top "&defrecordnum_
&" id,title,filename,k,imgsize,nameson from abc where id>"&cursorend&_
" order by id"
end select
set rs=conn.execute(sqlstr,0,1)
%>
<html>
<head>
<title>"无限流"分页程序  作者:arbiter</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<style type="text/css">td,br,div,p,body {font-size:12px}</style>
</head>
<body bgcolor="#ffffff" leftmargin="0" topmargin="0">
<table width="100%" border="0" cellspacing="0" cellpadding="3" bgcolor="#e2f5fe">
<tr align="center"> 
<td colspan="2"><%response.write curpagenum&"/"&totalpages&"页 总记录数:"&totalrecords%></td>
<td><a href="mllist.asp">首页</a> <a href=javascript:turnpage('back');>上一页</a> 
<a href=javascript:turnpage('next');>下一页</a> </td>
</tr>
</table>
<table width="100%" border="1" cellspacing="0" cellpadding="3" bgcolor="#cccccc">
<tr> 
<td>id</td>
<td>title</td>
<td>filename</td>
<td>大小</td>
<td>尺寸</td>
<td>类别</td>
</tr>
<%
turnpagefs(defrecordnum)
rs.close
set rs=nothing
conn.close
set conn=nothing
%> 
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="3" bgcolor="#e2f5fe">
<tr align="center"> 
<td colspan="2"><%response.write curpagenum&"/"&totalpages&"页 总记录数:"&totalrecords%></td>
<td><a href="mllist.asp">首页</a> <a href=javascript:turnpage('back');>上一页</a> 
<a href=javascript:turnpage('next');>下一页</a> </td>
</tr>
</table>
<%
endtime=timer
response.write "<br>程序执行时间:"&(endtime-begintime)*1000&"毫秒"
response.write " 第一条记录的id值(cursorbegin)="&cursorbegin&" "
response.write "最后一条记录的id值(cursorend)="&cursorend&"<br><br>"
%>
<script language="javascript">
function turnpage(func){
var curpagenum=<%=curpagenum%>; //取得当前页码
var cursorbegin=<%=cursorbegin%>; //取得第一个显示的记录的id值
var cursorend=<%=cursorend%>; //取得最后一个显示的记录的id值
var totalpages=<%=totalpages%>; //取得页面总数 
var backurl='mllist.asp?curpagenum='+(curpagenum-1)+'&cursorbegin='+cursorbegin+'&cursorend='+cursorend+'&hav=back';
var nexturl='mllist.
asp?curpagenum='+(curpagenum+1)+'&cursorbegin='+cursorbegin+'&cursorend='+cursorend+'&hav=next';
if(curpagenum<=1 && func=='back'){
location.href='#';
}else if(curpagenum>=totalpages && func=='next'){
location.href='#';
}else if(func=='back'){
location.href=backurl;
}else if(func='next'){
location.href=nexturl;
}
}
</script>
</body>
</html>  
asp?curpagenum='+(curpagenum+1)+'&cursorbegin='+cursorbegin+'&cursorend='+cursorend+'&hav=next';
if(curpagenum<=1 && func=='back'){
location.href='#';
}else if(curpagenum>=totalpages && func=='next'){
location.href='#';
}else if(func=='back'){
location.href=backurl;
}else if(func='next'){
location.href=nexturl;
}
}
</script>
</body>
</html>  
cnbruce的代码:
分页样例:[首页] [上页] [下页] [尾页] [页次:4/5页] [共86篇 20篇/页] 转到:_ 页
以下为公用代码,必须具备。
<%filepath=request.servervariables("path_info")%>
<%page=1 '设置变量初始值page=1
page=request.querystring("page") 'page值为接受值 
rs.pagesize = 20 '每页显示记录数
if not isempty(trim(request("page"))) then '如果page已经初始化...
page = cint(request("page")) '接收page并化为数字型赋给page变量 
if page > rs.pagecount then '如果接收的页数大于总页数
rs.absolutepage = rs.pagecount '设置当前显示页等于最后页 
elseif page <= 0 then '如果page小于等于0
page = 1 '设置page等于第一页
else
rs.absolutepage = page '如果大于零,显示当前页等于接收的页数 
end if
end if
page = rs.absolutepage%>
第一种分页
<%if rs.pagecount<>1 and rs.pagecount<>0 then%>'首先判断页总数不为1和0
<%if page>1 then%>
<%if page<rs.pagecount then %>
[<a href="<%=filepath%>?page=<% = 1%>">首页</a>]
[<a href="<%=filepath%>?page=<% = page -1 %>">上一页</a>]
[<a href="<%=filepath%>?page=<% = page + 1%>">下一页</a>]
[<a href="<%=filepath%>?page=<% = rs.pagecount%>">尾页</a>]
<%else%>
[<a href="<%=filepath%>?page=<% = 1%>">首页</a>]
[<a href="<%=filepath%>?page=<% = page -1 %>">上一页</a>] 
[下一页] [尾页]
<% end if %>
<%else%>
[首页] [上一页]
[<a href="<%=filepath%>?page=<% = page + 1%>">下一页</a>] 
[<a href="<%=filepath%>?page=<% = rs.pagecount%>">尾页</a>]
<%end if %>
<%else%>
[首页] [上一页] [下一页] [尾页]
<%end if%>
第二种分页
<%if rs.pagecount<>1 and rs.pagecount<>0 then%>
<%if page>1 then%>
[<a href="<%=filepath%>?page=<% = 1%>">首页</a>]
[<a href="<%=filepath%>?page=<% = page -1 %>">上一页</a>]
<%if page<rs.pagecount then %>
[<a href="<%=filepath%>?page=<% = page + 1%>">下一页</a>]
[<a href="<%=filepath%>?page=<% = rs.pagecount%>">尾页</a>] 
    <%else%>
    [下一页] [尾页]
<% end if %>
<%else%>
[首页] [上一页]
[<a href="<%=filepath%>?page=<% = page + 1%>">下一页</a>] 
[<a href="<%=filepath%>?page=<% = rs.pagecount%>">尾页</a>]
<%end if %>
<%else%>
[首页] [上一页] [下一页] [尾页]
<%end if%>
第三种
<%if rs.pagecount<>1 and rs.pagecount<>0 then%>
<%if page<rs.pagecount then%>
<%if page=1 then %>
[首页] [上一页]
<%else%>
[<a href="<%=filepath%>?page=<% = 1%>">首页</a>]
[<a href="<%=filepath%>?page=<% =page -1 %>">上一页</a>]
<% end if %>
[<a href="<%=filepath%>?page=<% = page + 1%>">下一页</a>] 
[<a href="<%=filepath%>?page=<% = rs.pagecount%>">尾页</a>]
<%else%>
[<a href="<%=filepath%>?page=<% = 1%>">首页</a>]
[<a href="<%=filepath%>?page=<% =page -1 %>">上一页</a>]
[下一页] [尾页]
<%end if %>
<%else%>
[首页] [上一页] [下一页] [尾页]
<%end if%>

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网