在做ASP程序时,很多地方要用到分页,重复作分页程序错误多,而且影响速度,所以我写一个分页函数,方便大家使用!速度,你测试了才知道
名称:超强分页函数(飞虎版)
作者:飞虎
QQ:290052621,624936300
发布日期:2009年12月24日(平安夜)
http://www.asp188.cn
http://it.asp188.cn
http://web.asp188.cn
本程序也带了数据库操作函数,见conn.asp文件
希望对初学者有点帮助,接下来我会把它编写成类,供大家使用!
Power by Asp188.Cn
下载地址:(硅谷动力)http://download.enet.com.cn/html/010042009122501.html
下载的压缩包里有数据库、数据库连接文件、分布函数文件,解压到根目录即可测试
速度是网络的生命线,大家自己测试吧!
程序预览:fhpage.asp
<!--#include file="conn.asp"-->
<%
'****************************************************
' 名称:超强分页函数(飞虎版)
' 作者:飞虎
' QQ:290052621,624936300
' 发布日期:2009年12月24日(平安夜)
' http://www.asp188.cn
' http://it.asp188.cn
' http://web.asp188.cn
' 本程序也带了数据库操作函数,见conn.asp文件
' 希望对初学者有点帮助,接下来我会把它编写成类,供大家使用!
' Power by Asp188.Cn
'****************************************************
Function WebPageList(ShowField, ShowTable, ShowWhere, ShowOrder, ShowKey)
Dim BeginTime '程序开始时间
Dim EndTime '程序结束时间
Dim SqlStr '查询字符串
Dim PageSizeN '每页记录数
Dim CursorBegin '开始ID号
Dim CursorEnd '结束ID号
Dim Act '命令(传递上一页下一页参数:Next,下一页;Back,上一页)
Dim Page '当前页码
Dim PageCountC '总页数
Dim TotalRecord '总记录数
Dim FilePath '本页路径
Dim IndexUrl '首页路径参数
Dim BackUrl '上一页路径参数
Dim NextUrl '下一页路径参数
Dim LastUrl '尾页路径参数
Dim SFnum,SFnumT,i,n
i=0
BeginTime=Timer
PageSizeN=20
Page=1
PageCountC=0
Act=Request("Act")
TotalRecord=0
TotalRecord=Request("TRecord")
ShowWhere=" and "&ShowWhere
If Request("CursorBegin")="" Then
CursorBegin=0
Else
CursorBegin=Request("CursorBegin")
End If
If Request("CursorEnd")="" Then
CursorEnd=0
Else
CursorEnd=Request("CursorEnd")
End If
ShowField=replace(ShowField,"contentquot;,",")
SFnum=split(ShowField,",")
SFnumT=Ubound(SFnum)
If Request("Page") <> "" Then
Page = Cint(Request("Page"))
End If
If Request("Page")<=0 Then
Act="index"
Page=1
End If
If Isnull(TotalRecord) or TotalRecord=0 Then
'---------统计总记录数/总页数---------
'推荐使用count(ID),因为ID为自动编号且索引,所以速度很快,否则会慢很多。首次读取数据时,此统计是最耗资源的,如果删除这一段,速度会快很多,不过只读取一次
SqlStr="Select count(ID) As RSUM From "&ShowTable
Set Rs=db(SqlStr,1)
TotalRecord=Rs("RSUM")
PageCountC=Abs(Int(TotalRecord/PageSizeN*(-1)))
Rs.Close
Set Rs=Nothing
Else
PageCountC=Abs(Int(TotalRecord/PageSizeN*(-1)))
End If
'-------------------------------------
If Act="" Then
Act="index"
End If
Select Case(Act)
Case "back"
CursorEnd=CursorBegin
SqlStr="Select Top "&PageSizeN&" "&ShowField&" From "&ShowTable&" Where 1=1"&ShowWhere&" and ID>"&CursorBegin&" And ID In (Select Top "&PageSizeN&" ID From "&ShowTable&" Where ID>"&CursorBegin&" order by "&ShowKey&" ASC) order by "&ShowKey&" "&ShowOrder
Case "next"
SqlStr="Select Top "&PageSizeN&" "&ShowField&" From "&ShowTable&" Where 1=1"&ShowWhere&" and ID>0 and ID<"&CursorEnd&" order by "&ShowKey&" "&ShowOrder
Case "index"
SqlStr="Select Top "&PageSizeN&" "&ShowField&" From "&ShowTable&" Where 1=1"&ShowWhere&" and ID>0 order by "&ShowKey&" "&ShowOrder
End Select
Set Rs=db(SqlStr,2)
Do While Not(Rs.Eof) And i<PageSizeN
i=i+1
Response.Write "<div id=""CursorContent"">"
for n=0 to SFnumT
Response.Write "<span>"&Rs(SFnum(n))&"</span>"
next
Response.Write "</div>"
If i=1 Then CursorBegin=Rs(0)
If i=PageSizeN or Rs.Eof Then CursorEnd=Rs(0)
Rs.MoveNext
Loop
Rs.Close
Set Rs=Nothing
Call conn_close
FilePath=Request.Servervariables("path_info")
IndexUrl=FilePath&"?TRecord="&TotalRecord&"&Act=index&Page=1"
BackUrl=FilePath&"?TRecord="&TotalRecord&"&CursorBegin="&CursorBegin&"&Act=back&Page="&(Page-1)&""
NextUrl=FilePath&"?TRecord="&TotalRecord&"&CursorBegin="&CursorEnd&"&CursorEnd="&CursorEnd&"&Act=next&Page="&(Page+1)&""
Response.Write("<div id=""pagelist"">")
Response.Write("[第"&Page&"页] ")
If TotalRecord>0 Then
Response.Write("[共"&TotalRecord&"条/总"&PageCountC&"页] ")
End If
If Page=1 Then
Response.Write("[首页] ")
Else
Response.Write("[<a Href="""&IndexUrl&""">首页</a>] ")
End If
If Page=1 Then
Response.Write("[上一页] ")
Else
Response.Write("[<a Href="""&BackUrl&""">上一页</a>] ")
End If
If i<PageSizeN Then
Response.Write("[下一页] ")
Else
Response.Write("[<a Href="""&NextUrl&""">下一页</a>] ")
End If
Response.Write("</div>")
EndTime=Timer
'Response.Write "<br>程序执行时间:"&(EndTime-BeginTime)*1000&"毫秒<br>"
End Function
%>
<style>
body{
margin:0;
padding:0;
width:100%;
}
#CursorContent{
width:100%;
height:20px;
line-height:20px;
text-align:center;
margin:0 auto;
font-size:12px;
}
#CursorContent span{
text-indent:5px;
}
#pagelist{
width:100%;
height:30px;
line-height:30px;
margin:0 auto;
font-size:12px;
text-align:center;
}
</style>
<%
'调用实例:Call WebPageList("id$guid", "FeiHuTestTable", "cat=18", "desc", "id") '字段(用$隔开),表,条件(不要where),排序(desc or asc),主键ID
Call WebPageList("id$guid", "FeiHuTestTable", "id>0", "desc", "id")
%>
预览图:
