ASP中如何使每页显示N条数据,然后有第2页—第N页选项

发布网友 发布时间:2022-04-22 00:37

我来回答

3个回答

热心网友 时间:2023-09-25 04:04

ASP项目中的公共翻页模块

<%
'+++++++++++++++++++++++++++++++++++++
'◆模块名称: 公共翻页模块
'◆文 件 名: 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
%>

热心网友 时间:2023-09-25 04:04

<<ASP分页代码>>

<!--#include file="conn.asp"--> '数据库路径配置文件

<%'=============分页定义开始,可放在数据库打开前或后
dim action
action=request.QueryString("action")
Const MaxPerPage=10 '定义每页显示记录数,可根据实际自定义
dim totalPut
dim CurrentPage
dim TotalPages
dim sql
if Not isempty(request("page")) then
currentPage=Cint(request("page"))
else
currentPage=1
end if
'=============分页定义结束%>

<% '=============打开数据库数据表,要以这种方式打开
set rs=server.CreateObject("adodb.recordset")
rs.open "select * from news where link=1 and shgg<>1 order by fincount desc",conn,1,1
%>

<%'=============分页类代码开始,需放在数据库数据表打开后

if err.number<>0 then
response.write "<p align='center'>数据库中暂时无数据</p>"
end if
if rs.eof And rs.bof then
Response.Write "<p align='center'>对不起,没有符合条件记录!</p>"
else
totalPut=rs.recordcount
if currentpage<1 then
currentpage=1
end if

if (currentpage-1)*MaxPerPage>totalput then
if (totalPut mod MaxPerPage)=0 then
currentpage= totalPut \ MaxPerPage
else
currentpage= totalPut \ MaxPerPage + 1
end if
end if

if currentPage=1 then
showContent
showpage totalput,MaxPerPage,""&request.ServerVariables("script_name")&""
else
if (currentPage-1)*MaxPerPage<totalPut then
rs.move (currentPage-1)*MaxPerPage

showContent
showpage totalput,MaxPerPage,""&request.ServerVariables("script_name")&""
else
currentPage=1
showContent

showpage totalput,MaxPerPage,""&request.ServerVariables("script_name")&""

end if
end if
end if
'=============分页类代码结束%>

<%'=============循环体开始
sub showContent
dim i
i=0
do while not rs.eof%>

<!---此处为内容循环输出---->

<%i=i+1
if i>=MaxPerPage then Exit Do
rs.movenext
loop
rs.close '释放资源
set rs=nothing
End Sub
'=============循环体结束%>

<%'=============放置分页显示开始
Function showpage(totalnumber,maxperpage,filename)
Dim n
If totalnumber Mod maxperpage=0 Then
n= totalnumber \ maxperpage
Else
n= totalnumber \ maxperpage+1
End If %>
<form method=Post action=<%=filename%>>
<p align="center">
<%If CurrentPage<2 Then %>
首 页 上一页
<% Else %>
<a href=<% = filename %>?page=1>首 页</a>
<a href=<% = filename %>?page=<% = CurrentPage-1 %>>上一页</a>
<% End If
If n-currentpage<1 Then %>
下一页 尾 页
<% Else %>
<a href=<% = filename %>?page=<% = (CurrentPage+1) %>>下一页</a>
<a href=<% = filename %>?page=<% = n %>>尾 页</a>
<% End If %>
页次:<b><font color=red><% = CurrentPage %></font></b>/<b><% = n %></b>页 <b><%=maxperpage%></b>个记录/页 共<b><%=totalnumber %></b>个记录
转到:<select name="cndok" onchange="javascript:location=this.options[this.selectedIndex].value;">
<%
for i = 1 to n
if i = CurrentPage then%>
<option value="<% = filename %>?page=<%=i%>" selected>第<%=i%>页</option>
<%else%>
<option value="<% = filename %>?page=<%=i%>">第<%=i%>页</option>
<%
end if
next
%>
</select></font>
</form>
<%End Function
'=============放置分页显示结束%>

不知道你是否能读得懂!不懂的话可以加我!

热心网友 时间:2023-09-25 04:05

请参考: http://www.aspjs.cn/Article/ASPJC/200409/39.html

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com