在大型的ASP项目中,很多的页面都涉及到翻页功能。如果每个页面都写一个翻页的程序的话,这样的工作即降低了工作效率,也不利于工程的模块化,不能使代码重用。因此,把翻页这样的功能模块化是很有必要的。
设计方法:
1、调用该模块时,只需要传递记录集和每页显示的记录的条数;
2、可以点击链接进行翻页,也可以直接输入页码,回车后翻页;
3、不要考虑文件名,程序的每次翻页都能在当前页面。
想清楚了上面3个问题,我们的公共翻页模块就可以动手了。
<%
'+++++++++++++++++++++++++++++++++++++
'◆模块名称: 公共翻页模块
'◆文 件 名: TurnPage.asp
'◆传入参数: Rs_tmp (记录集), PageSize (每页显示的记录条数)
'◆输 出: 记录集翻页显示功能
'+++++++++++++++++++++++++++++++++++++
'
Sub TurnPage(ByRef Rs_tmp,PageSize) 'Rs_tmp 记录集 ; PageSize 每页显示的记录条数;
Dim TotalPage '总页数
Dim PageNo '当前显示的是第几页
Dim RecordCount '总记录条数
Rs_tmp.PageSize = PageSize
RecordCount = Rs_tmp.RecordCount
TotalPage = INT(RecordCount / PageSize * -1)*-1
PageNo = Request.QueryString ("PageNo")
'直接输入页数跳转;
If Request.Form("PageNo")<>"" Then PageNo = Request.Form("PageNo")
'如果没有选择第几页,则默认显示第一页;
If PageNo = "" then PageNo = 1
If RecordCount <> 0 then
Rs_tmp.AbsolutePage = PageNo
End If
'获取当前文件名,使得每次翻页都在当前页面进行;
Dim fileName,postion
fileName = Request.ServerVariables("script_name")
postion = InstrRev(fileName,"/")+1
'取得当前的文件名称,使翻页的链接指向当前文件;
fileName = Mid(fileName,postion)
%>
<table border=0 width='100%'>
<tr>
<td align=left> 总页数:<font color=#ff3333><%=TotalPage%></font>页
当前第<font color=#ff3333><%=PageNo%></font>页</td>
<td align="right">
<%If RecordCount = 0 or TotalPage = 1 Then
Response.Write "首页|前页|后页|末页"
Else%>
<a href="<%=fileName%>?PageNo=1">首页|</a>
<%If PageNo - 1 = 0 Then
Response.Write "前页|"
Else%>
<a href="<%=fileName%>?PageNo=<%=PageNo-1%>">前页|</a>
<%End If
If PageNo+1 > TotalPage Then
Response.Write "后页|"
Else%>
<a href="<%=fileName%>?PageNo=<%=PageNo+1%>">后页|</a>
<%End If%>
<a href="<%=fileName%>?PageNo=<%=TotalPage%>">末页</a>
<%End If%></td>
<td width=95>转到第
<%If TotalPage = 1 Then%>
<input type=text name=PageNo size=3 readonly disabled style="background:#d3d3d3">
<%Else%>
<input type=text name=PageNo size=3 value="" title=请输入页号,然后回车>
<%End If%>页
</td>
</tr>
</table>
<%End Sub%>
当然,大家可以把翻页的链接做成图片按钮,这样的话也面就更加美观了。
调用方法:
1、在程序开始或要使用翻页的地方包含翻页模块文件;
2、定义变量:RowCount,每页显示的记录条数
3、调用翻页过程:Call TurnPage(记录集,RowCount)
4、在Do While 循环输出记录集的条件中加上" RowCount > 0 " 条件
5、在循环结束 "Loop前" 加上: RowCount = RowCount - 1
'-----------------------------------------------------
调用范例:
文件名:News.asp
<%
Dim Conn,Rs_News
Set Conn = server.CreateObject("ADODB.CONNECTION")
Conn.Open "cpm","cpm","cpm"
Dim Sql
Sql = "Select * from News"
Set Rs_News = Server.CreateObject("ADODB.RECORDSET")
Rs_News.Open Sql,Conn,1,3 '获取的记录集
'公共翻页模块开始%>
<!--#include file=../Public/TurnPage.asp-->
<%
Dim RowCount
RowCount = 10 '每页显示的记录条数
Call TurnPage(Rs_News,RowCount)
'公共翻页模块结束%>
<table width=100%>
<tr>
<td>新闻编号</td>
<td>新闻标题</td>
<td>发布日期</td>
<tr>
<%
If Not Rs_News.eof
Do while Not Rs_News.eof and RowCount>0
%>
<tr>
<td><%=Rs_News("ID")%></td>
<td><%=Rs_News("Name")%></td>
<td><%=Rs_News("Date")%></td>
<tr>
<%
RowCount = RowCount - 1
Rs_News.MoveNext
Loop
End If
%>
<%sub list_page(TotalRec,PageSize)
'TotalTec:总记录数
'PageSize:每页显示多少条记录
TotalPage=Fix((TotalRec-1)/PageSize)+1 '得到总页数
CurrentPage=Request("page") '获得当前页
if CurrentPage="" or not isInteger(CurrentPage) then
CurrentPage=1
else
CurrentPage=clng(CurrentPage)
end if
P=(CurrentPage-1) /10
response.write"<form method=GET>页次:<b>"& CurrentPage &"</b>/<b>"& TotalPage &"</b>页 每页<b>"&PageSize&"</b> 总记录:<b>"& TotalRec &"</b></td>"&_
"<td valign=middle><div align=right >分页:"
if CurrentPage=1 then
response.write "<font face=webdings color=red>9</font>"
else
response.write "<a href='?page=1"' title=首页><font face=webdings>9</font></a> "
end if
if p*10>0 then response.write "<a href='?page="&Cstr(p*10)&"' title=上10页><font face=webdings>7</font></a> "
response.write "<b>"
for ii=p*10+1 to P*10+10
if ii=currentPage then
response.write "<font color=red>"+Cstr(ii)+"</font> "
else
response.write "<a href='?page="&Cstr(ii)&"'>"+Cstr(ii)+"</a> "
end if
if ii=TotalPage then exit for
next
response.write "</b>"
if ii<n then response.write "<a href='?page="&Cstr(ii)&"' title=下十页><font face=webdings>8</font></a> "
if currentPage=TotalPage then
response.write "<font face=webdings color=red>:</font> "
else
response.write "<a href='?page="&Cstr(TotalPage)&"' title=尾页><font face=webdings>:</font></a> "
end if
response.write "转到:<input type=text name=Page size=3 maxlength=10 value="& currentpage &"><input type=button value=Go name=submit></form>"
end sub%>
'ASP通用翻页函数 作者:天地小子 2005.7.7 twt326@163.com
'输入查询语句,数据库名,数据库连接,当前页号,链接字符串,列表条数,查询条件(默认为空)
'显示上下页导航链接,有下拉框显示页数
'这个函数是根据天地PHP通用翻页函数1直接转变的,其中使用了CSDN BBS上一个大虾的SQL高效分页方案技术
':)由于这里没有找到那篇帖子,所以老兄请勿见怪了。配合下面的函数使用起来更方便
sub getnav(dbname,conn,tnowpage,link,pagelistnum,wherewords)
dim sql,trst,sumrows,pagesum,tmphead,showdown,selectcode,i
sql="select count(*) as recnum from "&dbname
if wherewords<>"" then sql=sql & " where " & wherewords
set trst=Server.CreateObject("adodb.recordset")
trst.open sql,conn,1,1
sumrows=trst("recnum")
pagesum=sumrows\pagelistnum+1
if sumrows mod pagelistnum=0 then pagesum=sumrows/pagelistnum
tmphead="<a href="&link&"&page="
if cint(tnowpage)<2 then
showdown="<table><form name='selform' method='post' action=''><tr><td>首页 上页 "
else
showdown="<table><form name='selform' method='post' action=''><tr><td>"&tmphead&"1>首页</a> "&tmphead&(tnowpage-1)&">上页</a> "
end if
if cint(tnowpage)<cint(pagesum) then
showdown=showdown&tmphead&(tnowpage+1)&">下页</a> "&tmphead&pagesum&">末页</a> "
else
showdown=showdown&"下页 末页 "
end if
'获取下拉框转向代码
selectcode="<script language='javascript'>function gopagenav(page){ location='"&link&"&page='+page; }</script><select name='selpage' id='selpage' onChange='javascript:gopagenav(this.value);'>"
for i=1 to pagesum
selectcode=selectcode&"<option value='"&i&"'"
if cInt(i)=cint(tnowpage) then selectcode=selectcode&" selected"
selectcode=selectcode&">= "&i&" =</option>"
next
selectcode=selectcode&"</select>"
showdown=showdown&"第</td><td>"&selectcode&"</td><td>页,共"&pagesum&"页 总记录数:"&sumrows&"</td></tr></form></table>"
response.write showdown
trst.close
set trst=nothing
end sub
'根据参数生成查询语句,查询某页数据
'参数:当前页,每页显示条数,数据表名(多个用逗号隔开),需要选择的字段名(用逗号隔开),查询条件(无条件时为空字串),ID主键,排序方式(ASC正序或DESC倒序)
'使用示例
' sql=getfysql(page,pagelistnum,"tbl_message,tbl_login","tbl_message.*,tbl_login.l_user","tbl_message.m_lid=tbl_login.l_id","m_id","DESC")
' getnav "tbl_message",conn,page,"adminmessage.asp?act=",pagelistnum,addnavwords
function getfysql(tnowpage,tpagelistnum,tblname,selfields,wherewords,keyfield,sorttype)
dim fh,hs
if wherewords="" then wherewords="1=1"
fh=">":hs="max"
if sorttype="DESC" then
fh="<"
hs="min"
end if
if (tnowpage-1)*tpagelistnum=0 then
sql="select top "&tpagelistnum&" "&selfields&" from "&tblname&" where "&wherewords&" order by "&keyfield&" "&sorttype
else
sql="select top "&tpagelistnum&" "&selfields&" from "&tblname&" where "&wherewords& " and " &keyfield&fh&"(select "&hs&"("&keyfield&") from (select top "&((tnowpage-1)*tpagelistnum)&" "&keyfield&" from "&tblname&" where "&wherewords& " order by "&keyfield&" "&sorttype&") as T) order by "&keyfield&" "&sorttype
end if
getfysql=sql
end function