ASP项目中的公共翻页模块
2023-06-13 09:13:47 时间
在大型的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
%>
修正:
<%
If Not Rs_News.eof then
Do while Not Rs_News.eof and RowCount>0
%>
而那个公共模块缺<form>,改后:
<%
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 width="258" align=left> 总页数:<font color=#ff3333><%=TotalPage%></font>页
当前第<font color=#ff3333><%=PageNo%></font>页 总共<%=RecordCount%>条 </td>
<td width="308" align="right"> <div align="center">
<%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%>
</div></td>
<td width=189><form name="form1" method="post" action=""> 转到第 <% If TotalPage = 1 Then%>
<input type=text name=PageNo size=3 readonly disabled style="background:#d3d3d3">
<input type="submit" name="Submit" value="Go" disabled style="background:#d3d3d3">
<%Else%>
<input type=text name=PageNo size=3 >
<input type="submit" name="Submit" value="Go">
<%End If%>
</form>
页
</td>
</tr>
</table>
<%End Sub%>
设计方法:
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
%>
修正:
<%
If Not Rs_News.eof then
Do while Not Rs_News.eof and RowCount>0
%>
而那个公共模块缺<form>,改后:
<%
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 width="258" align=left> 总页数:<font color=#ff3333><%=TotalPage%></font>页
当前第<font color=#ff3333><%=PageNo%></font>页 总共<%=RecordCount%>条 </td>
<td width="308" align="right"> <div align="center">
<%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%>
</div></td>
<td width=189><form name="form1" method="post" action=""> 转到第 <% If TotalPage = 1 Then%>
<input type=text name=PageNo size=3 readonly disabled style="background:#d3d3d3">
<input type="submit" name="Submit" value="Go" disabled style="background:#d3d3d3">
<%Else%>
<input type=text name=PageNo size=3 >
<input type="submit" name="Submit" value="Go">
<%End If%>
</form>
页
</td>
</tr>
</table>
<%End Sub%>
相关文章
- Java项目毕业设计:基于springboot+vue的电影视频网站系统「建议收藏」
- 数据库使用 ASP 读取 MySQL 数据库的实现方法(asp读取mysql)
- ASP.NET WebForm也可以这样用Ajax(程序猿,你知道不)
- ASP空间与MSSQL相结合,完美解决网站数据库存储与运行问题(asp空间mssql)
- ASP程序查询MSSQL数据库的实践经验(asp查询mssql)
- 用MSSQL查询ASP:从初学者到高手(mssql查询asp)
- ASP连接MySQL实现数据交互的操作语句(asp连接mysql语句)
- ASP转换MySQL掌握数据库迁移技术(asp转换为mysql)
- 快速实现网站功能,利用aspmysql源码(asp源码mysql)
- 库ASP实现MySQL数据库数据更新(asp更新mysql数据)
- ASP构建MySQL数据库从零出发(asp数据库mysql)
- ASP与MySQL结合最佳网站开发方案(asp接mysql)
- 快速了解如何在ASP环境下安装MySQL(asp 安装mysql)
- 利用ASP技术进行MySQL数据库开发(asp可以使用mysql)
- ASP编程操作MySQL二进制字段(asp二进制mysql)
- 在Asp.net用C#建立动态Excel
- ASP动态生成的javascript表单验证代码
- 利用ASP发送和接收XML数据的处理方法与代码
- 用Asp隐藏文件路径,实现防盗链的代码
- Asp.netXML文档进行添加删改操作的实例代码
- asp.net中各种类型的JSON格式化
- asp.net身份验证方式介绍
- 浅析ASP.NET安全性分析(加强asp.net1.1/2.0安全性)
- asp.net判断字符串是否是中文的方法
- asp.net服务器端指令include的使用及优势介绍
- asp.net操作ini文件示例