╃苍狼山庄╃╃苍狼山庄╃

Clang出品,
必属精品!

公共翻页模块

设计方法:
1、调用该模块时,只需要传递记录集和每页显示的记录的条数;
2、可以点击链接进行翻页,也可以直接输入页码,回车后翻页;
3、不要考虑文件名,程序的每次翻页都能在当前页面。

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

<%
'翻页函数 pagein.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


Dim MyVar
Dim MyCheck

Dim MyVar1
Dim MyCheck1

if Request.QueryString ("PageNo")="" then
PageNo = Request.QueryString ("PageNo")
end if


if Request.QueryString ("PageNo")<>"" then
MyVar =Request.QueryString ("PageNo")
MyCheck = IsNumeric(MyVar)
 if MyCheck=True then
 PageNo = Cint(Request.QueryString ("PageNo"))
 else
 PageNo = 1
 end if
end if

'直接输入页数跳转;
If Request.Form("PageNo")<>"" Then
MyVar1 =Request.Form("PageNo")
MyCheck1 = IsNumeric(MyVar1)
 if MyCheck1=True then
 PageNo = Cint(Request.Form("PageNo"))
 else
 PageNo = 1
 end if
end if
'如果没有选择第几页,则默认显示第一页;
If PageNo = "" then PageNo = 1
If RecordCount <> 0 then
Rs_tmp.AbsolutePage = PageNo
End If

'获取当前文件名,使得每次翻页都在当前页面进行;
Dim fileName,postion,fileString
fileName = Request.ServerVariables("script_name")
fileString = Request.ServerVariables("QUERY_STRING")
postion = InstrRev(fileName,"/")+1
'取得当前的文件名称,使翻页的链接指向当前文件;
fileName = Mid(fileName,postion)
%>

method=get>

<%=TotalPage%> 页 /
<%=RecordCount%>
当前 第 <%=PageNo%> 页 每页 <%=PageSize%>
<%If RecordCount = 0 or TotalPage = 1 Then
Response.Write "首页 | 前页 | 后页 | 末页"
Else%> 首页 | <%If PageNo - 1 = 0 Then
Response.Write "前页 |"
Else%> 前页 | <%End If

If PageNo+1 > TotalPage Then
Response.Write "后页 |"
Else%> 后页 |
<%End If%> 末页
<%End If%>
转到第
<%If TotalPage = 1 Then%>

<%Else%>

<%End If%>
<%If TotalPage <> 1 Then%>
<%End If%>
<%End Sub%>

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

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

使用范例:

1.首先请将以上代码框内代码转存为pagein.asp

2.我们新建一个ASP页面,内容如下

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>

 '引入公共翻页模块
<% '定义记录集
Dim rsnews
Dim rsnews_numRows

Set rsnews = Server.CreateObject("ADODB.Recordset")
rsnews.ActiveConnection = MM_conn_STRING
rsnews.Source = "Select * FROM Table Where nisshow = 1 orDER BY naddtime DESC"
rsnews.CursorType = 1
rsnews.CursorLocation = 2
rsnews.LockType = 1
rsnews.Open()

rsnews_numRows = 0
%>
<% '使用重复行为(重复下面的标签内容)
Dim Repeat2__numRows
Dim Repeat2__index

Repeat2__numRows = 20
Repeat2__index = 0
rsnews_numRows = rsnews_numRows + Repeat2__numRows
%>


<% '关键在这里,在重复内容上面产生一个导航条
dim RowCount
RowCount = 20
call TurnPage(rsnews,RowCount)
%>
<% '重复开始 While ((Repeat2__numRows <> 0) AND (NOT rsnews.EOF)) %> <% Repeat2__index=Repeat2__index+1 Repeat2__numRows=Repeat2__numRows-1 rsnews.MoveNext() Wend '重复结束 %>
<% =(rsnews.Fields.Item("ntitle").Value) %>
<% '在重复内容下面产生一个导航条,可选,注意这里不再需要dim RowCount
RowCount = 20
call TurnPage(rsnews,RowCount)
%>
<% '关闭记录集 rsnews.Close() Set rsnews = Nothing %>

PS: 当用户输入不存在的页数时(如小数,负数等) 程序将自动过滤.

本原创文章未经允许不得转载 | 当前页面:╃苍狼山庄╃ » 公共翻页模块

评论

文章评论已关闭!