ASP项目中的公共翻页模块

时间:2021-05-28

在大型的ASP项目中,很多的页面都涉及到翻页功能。如果每个页面都写一个翻页的程序的话,这样的工作即降低了工作效率,也不利于工程的模块化,不能使代码重用。因此,把翻页这样的功能模块化是很有必要的。
设计方法:
1、调用该模块时,只需要传递记录集和每页显示的记录的条数;
2、可以点击链接进行翻页,也可以直接输入页码,回车后翻页;
3、不要考虑文件名,程序的每次翻页都能在当前页面。

想清楚了上面3个问题,我们的公共翻页模块就可以动手了。

<%
'+++++++++++++++++++++++++++++++++++++
'◆模块名称:公共翻页模块
'◆文件名:TurnPage.asp
'◆传入参数:Rs_tmp(记录集),PageSize(每页显示的记录条数)
'◆输出:记录集翻页显示功能
'+++++++++++++++++++++++++++++++++++++
'
SubTurnPage(ByRefRs_tmp,PageSize)'Rs_tmp记录集;PageSize每页显示的记录条数;
DimTotalPage'总页数
DimPageNo'当前显示的是第几页
DimRecordCount'总记录条数
Rs_tmp.PageSize=PageSize
RecordCount=Rs_tmp.RecordCount
TotalPage=INT(RecordCount/PageSize*-1)*-1
PageNo=Request.QueryString("PageNo")
'直接输入页数跳转;
IfRequest.Form("PageNo")<>""ThenPageNo=Request.Form("PageNo")
'如果没有选择第几页,则默认显示第一页;
IfPageNo=""thenPageNo=1
IfRecordCount<>0then
Rs_tmp.AbsolutePage=PageNo
EndIf

'获取当前文件名,使得每次翻页都在当前页面进行;
DimfileName,postion
fileName=Request.ServerVariables("script_name")
postion=InstrRev(fileName,"/")+1
'取得当前的文件名称,使翻页的链接指向当前文件;
fileName=Mid(fileName,postion)
%>
<tableborder=0width='100%'>
<tr>
<tdalign=left>总页数:<fontcolor=#ff3333><%=TotalPage%></font>页
当前第<fontcolor=#ff3333><%=PageNo%></font>页</td>
<tdalign="right">
<%IfRecordCount=0orTotalPage=1Then
Response.Write"首页|前页|后页|末页"
Else%>
<ahref="<%=fileName%>?PageNo=1">首页|</a>
<%IfPageNo-1=0Then
Response.Write"前页|"
Else%>
<ahref="<%=fileName%>?PageNo=<%=PageNo-1%>">前页|</a>
<%EndIf

IfPageNo+1>TotalPageThen
Response.Write"后页|"
Else%>
<ahref="<%=fileName%>?PageNo=<%=PageNo+1%>">后页|</a>
<%EndIf%>

<ahref="<%=fileName%>?PageNo=<%=TotalPage%>">末页</a>
<%EndIf%></td>
<tdwidth=95>转到第
<%IfTotalPage=1Then%>
<inputtype=textname=PageNosize=3readonlydisabledstyle="background:#d3d3d3">
<%Else%>
<inputtype=textname=PageNosize=3value=""title=请输入页号,然后回车>
<%EndIf%>页
</td>
</tr>
</table>
<%EndSub%>

当然,大家可以把翻页的链接做成图片按钮,这样的话也面就更加美观了。

调用方法:
1、在程序开始或要使用翻页的地方包含翻页模块文件;
2、定义变量:RowCount,每页显示的记录条数
3、调用翻页过程:CallTurnPage(记录集,RowCount)
4、在DoWhile循环输出记录集的条件中加上"RowCount>0"条件
5、在循环结束"Loop前"加上:RowCount=RowCount-1

'-----------------------------------------------------
调用范例:
文件名:News.asp

<%
DimConn,Rs_News
SetConn=server.CreateObject("ADODB.CONNECTION")
Conn.Open"cpm","cpm","cpm"

DimSql
Sql="Select*fromNews"
SetRs_News=Server.CreateObject("ADODB.RECORDSET")
Rs_News.OpenSql,Conn,1,3'获取的记录集

'公共翻页模块开始%>
<!--#includefile=../Public/TurnPage.asp-->
<%
DimRowCount
RowCount=10'每页显示的记录条数
CallTurnPage(Rs_News,RowCount)
'公共翻页模块结束%>

<tablewidth=100%>
<tr>
<td>新闻编号</td>
<td>新闻标题</td>
<td>发布日期</td>
<tr>
<%
IfNotRs_News.eof
DowhileNotRs_News.eofandRowCount>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
EndIf
%>



修正:
<%
IfNotRs_News.eofthen
DowhileNotRs_News.eofandRowCount>0
%>

而那个公共模块缺<form>,改后:
<%
SubTurnPage(ByRefRs_tmp,PageSize)'Rs_tmp记录集;PageSize每页显示的记录条数;
DimTotalPage'总页数
DimPageNo'当前显示的是第几页
DimRecordCount'总记录条数
Rs_tmp.PageSize=PageSize
RecordCount=Rs_tmp.RecordCount
TotalPage=INT(RecordCount/PageSize*-1)*-1
PageNo=Request.QueryString("PageNo")
'直接输入页数跳转;
IfRequest.Form("PageNo")<>""ThenPageNo=Request.Form("PageNo")
'如果没有选择第几页,则默认显示第一页;
IfPageNo=""thenPageNo=1
IfRecordCount<>0then
Rs_tmp.AbsolutePage=PageNo
EndIf
'获取当前文件名,使得每次翻页都在当前页面进行;
DimfileName,postion
fileName=Request.ServerVariables("script_name")
postion=InstrRev(fileName,"/")+1
fileName=Mid(fileName,postion)
%>
<tableborder=0width='100%'>
<tr>
<tdwidth="258"align=left>总页数:<fontcolor=#ff3333><%=TotalPage%></font>页
当前第<fontcolor=#ff3333><%=PageNo%></font>页总共<%=RecordCount%>条</td>
<tdwidth="308"align="right"><divalign="center">
<%IfRecordCount=0orTotalPage=1Then
Response.Write"首页|前页|后页|末页"
Else%>
<ahref="<%=fileName%>?PageNo=1">首页|</a>
<%IfPageNo-1=0Then
Response.Write"前页|"
Else%>
<ahref="<%=fileName%>?PageNo=<%=PageNo-1%>">前页|</a>
<%EndIf

IfPageNo+1>TotalPageThen
Response.Write"后页|"
Else%>
<ahref="<%=fileName%>?PageNo=<%=PageNo+1%>">后页|</a>
<%EndIf%>
<ahref="<%=fileName%>?PageNo=<%=TotalPage%>">末页</a>
<%EndIf%>
</div></td>
<tdwidth=189><formname="form1"method="post"action="">转到第<%IfTotalPage=1Then%>
<inputtype=textname=PageNosize=3readonlydisabledstyle="background:#d3d3d3">
<inputtype="submit"name="Submit"value="Go"disabledstyle="background:#d3d3d3">
<%Else%>
<inputtype=textname=PageNosize=3>
<inputtype="submit"name="Submit"value="Go">
<%EndIf%>
</form>

</td>
</tr>
</table>
<%EndSub%>

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章