今天我写了个分页函数(ASP+ACCESS),之前在网上搜了下,很多,但是好多都是一大堆的代码,有的的确很强大,杀鸡焉用宰牛刀,所以自己写了个,代码没几行,但是速度也很快。一百万条数据,页面执行时间也就几十毫秒。
我写的这个函数,显示效果有两种。第一种就是5页内,页码左右有“上一页”和“下一页”;第二种是超过5页,页码左右是“前5页”和“后5页”。效果图如下图:
pagination.inc复制内容到剪贴板
代码:
<%
Function pagination(pagecount)
dim myPage,myPageCount,PageNum
if Len(Request.QueryString("page"))<>0 then
myPage = clng(Request.QueryString("page"))
else
myPage =1
End if
if myPage <= 0 then myPage =1
PageNum = (myPage \ 5)*5+1
if myPage mod 5 = 0 then PageNum = (myPage \ 5)*5-4
if myPage <= pagecount then
if myPage > 5 then
Response.Write ("<a href=""?page=1"">首页</a>")
Response.Write (" <a href=""?page="& PageNum-1 &""">前5页</a>")
elseif myPage > 1 and pagecount <= 5 then
Response.Write ("<a href=""?page="& myPage-1 &""">上一页</a>")
End if
for PageNum = PageNum To PageNum + 4
if PageNum = myPage then
Response.Write (" <strong>["& PageNum &"]</strong>")
else
Response.Write (" <a href=""?page="& PageNum &""">")
Response.Write ("["& PageNum &"]")
Response.Write ("</a>")
End if
if PageNum >= pagecount then Exit for
Next
End if
if myPage <= (pagecount - (pagecount mod 5)) and pagecount > 5 then
Response.Write (" <a href=""?page="& PageNum &""">后5页</a>")
Response.Write (" <a href=""?page="& pagecount &""">末页</a>")
elseif myPage < pagecount and pagecount <= 5 then
Response.Write (" <a href=""?page="& myPage+1 &""">下一页</a>")
End if
End Function
%>Pagination.asp复制内容到剪贴板
代码:
<%
//ASP分页函数
//===============================================
//版权所有 (C) 2008 wo_is神仙,并保留所有权利。
//-----------------------------------------------
//请保留此段代码,它不会影响网站运行速度
//-----------------------------------------------
//Author:wo_is神仙
//Date:2008-3-20 13:42:48 +0800 星期四
//===============================================
dim startime,endtime
startime=timer()
%>
<!--#include file="conn.inc"-->
<!--#include file="pagination.inc"-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>分页</title>
<link href="pagination.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<%dim myPageSize,rsCount,myPageCount,myPage
myPageSize = 20
rsCount = conn.execute ("select count(id) from test",0,1)(0)
myPageCount = abs(int(-abs(rsCount/myPageSize)))
myPage = clng(Request.QueryString("page"))
if len(myPage) = 0 or myPage = 0 then myPage = 1
%>
<table width="780" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr bgcolor="#FFFFFF">
<td height="25" align="center">ID</td>
<td align="center">标题</td>
<td align="center">内容</td>
<td align="center">时间</td>
</tr>
<%set rs = Server.CreateObject("adodb.recordset")
rs.Open "select * from test order by id desc",conn,1,1
if rs.EOF or rs.BOF then%>
<tr>
<td height="25" colspan="4" align="center" bgcolor="#FFFFFF">暂无内容</td>
</tr>
<%else
//根据分页参数获取当前页面纪录
rs.AbsolutePosition=rs.AbsolutePosition+((abs(myPage)-1)*myPageSize)
dim i,bgcolor
for i = 0 to myPageSize-1
if rs.EOF then exit for
bgColor="#FFFFFF"
if i mod 2=0 then bgColor="#f1f1f1"
%>
<tr bgcolor="<%=bgColor%>">
<td height="25" align="center"><%=rs(0)%></td>
<td align="center"><%=rs(1)%></td>
<td align="center"><%=rs(2)%></td>
<td align="center"><%=rs(3)%></td>
</tr>
<%
rs.MoveNext
next
End if
rs.Close
set rs = nothing
%>
</table>
<table width="780" border="0" align="center" cellpadding="0" cellspacing="5">
<tr>
<td width="30%" height="25"><%="共" & rsCount & "条 " & myPageSize & "条/页 共" & myPageCount & "页"%></td>
<td width="70%" height="25" align="right"><%=pagination(myPageCount)%></td>
</tr>
<tr>
<td height="25" colspan="2" align="center">
<%endtime=timer()%>
本页面执行时间:<%=formatnumber((Endtime-startime)*1000,3)%>毫秒</td>
</tr>
</table>
</body>
</html>
<%closeconn()%>测试数据(一百万条数据)太大,传不上来,下载地址:http://jsw0528.b77.53dns.com/test.rar
[
本帖最后由 jsw0528 于 2008-3-24 15:50 编辑 ]