ASP公共翻页模块
用DWMX2004的插件包可以实现ASP的各种分页功能,但是那些代码实在有点恐怖,我们在实际的项目开发中,有没有更简单的方法实现"一劳永逸"呢?答案是肯定的.
我们需要的功能:
1、调用该模块时,只需要传递记录集和每页显示的记录的条数;
2、可以点击链接进行翻页,也可以直接输入页码,回车后翻页;
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)
%>
<script language="JavaScript">
<!--
function test(theForm){
var thisone
thisone = <%=TotalPage%>
if (theForm.PageNo.value == "" || isNaN(theForm.PageNo.value) )
{
alert("请输入一个页数");
theForm.PageNo.focus();
return (false);
}
if (theForm.PageNo.value > thisone || theForm.PageNo.value < 1)
{
alert("不存在此页");
theForm.PageNo.focus();
return (false);
}
return true;
}
// --></script>
<table width='98%' border=0 align="right" cellpadding="0" cellspacing="0" class="12font">
<tr>
<td width="45%" height="25" align=left> 共 <font color=#ff3333><%=TotalPage%></font> 页 /
<font color=#ff3333><%=RecordCount%></font> 条
当前 第 <font color=#ff3333><%=PageNo%></font> 页 每页 <font color="#ff3333">
<%=PageSize%></font> 条</td>
<td width="27%" 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%></td>
<form onSubmit="return test(this);" name=theForm action=<%=fileName%> method=get>
<td width="26%" align=right>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="12font">
<tr>
<td width="1%"> </td>
<td width="67%">转到第
<%If TotalPage = 1 Then%>
<input type=text name=PageNo size=3 readonly disabled style="background:#d3d3d3" class=myinput>
<%Else%>
<input type=text name=PageNo size=3 value="" class=myinput maxlength=4 title=请输入页号,然后回车>
<%End If%>
页 </td>
<td width="32%" valign="top"> <%If TotalPage <> 1 Then%>
<input type="submit" alt="跳转显示" value="go" name=button ><%End If%> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<%End Sub%>
'翻页函数 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)
%>
<script language="JavaScript">
<!--
function test(theForm){
var thisone
thisone = <%=TotalPage%>
if (theForm.PageNo.value == "" || isNaN(theForm.PageNo.value) )
{
alert("请输入一个页数");
theForm.PageNo.focus();
return (false);
}
if (theForm.PageNo.value > thisone || theForm.PageNo.value < 1)
{
alert("不存在此页");
theForm.PageNo.focus();
return (false);
}
return true;
}
// --></script>
<table width='98%' border=0 align="right" cellpadding="0" cellspacing="0" class="12font">
<tr>
<td width="45%" height="25" align=left> 共 <font color=#ff3333><%=TotalPage%></font> 页 /
<font color=#ff3333><%=RecordCount%></font> 条
当前 第 <font color=#ff3333><%=PageNo%></font> 页 每页 <font color="#ff3333">
<%=PageSize%></font> 条</td>
<td width="27%" 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%></td>
<form onSubmit="return test(this);" name=theForm action=<%=fileName%> method=get>
<td width="26%" align=right>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="12font">
<tr>
<td width="1%"> </td>
<td width="67%">转到第
<%If TotalPage = 1 Then%>
<input type=text name=PageNo size=3 readonly disabled style="background:#d3d3d3" class=myinput>
<%Else%>
<input type=text name=PageNo size=3 value="" class=myinput maxlength=4 title=请输入页号,然后回车>
<%End If%>
页 </td>
<td width="32%" valign="top"> <%If TotalPage <> 1 Then%>
<input type="submit" alt="跳转显示" value="go" name=button ><%End If%> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<%End Sub%>
大家可以把翻页的链接做成图片按钮,这样的话也面就更加美观了。
调用方法:
1、在程序开始或要使用翻页的地方包含翻页模块文件;
2、定义变量:RowCount恳诚允镜募锹继跏?
3、调用翻页过程:Call TurnPage(记录集,RowCount)
4、在Do While 循环输出记录集的条件中加上" RowCount > 0 " 条件
5、在循环结束 "Loop前" 加上: RowCount = RowCount - 1
使用范例:
1.首先请将以上蓝框内代码转存为pagein.asp
2.我们使用DWMX2004新建一个ASP页面,内容如下
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#i nclude file="Connections/conn.asp" -->
<!--#i nclude file="pagein.asp"--> '引入公共翻页模块
<% '定义记录集
Dim rsnews
Dim rsnews_numRows
Set rsnews = Server.CreateObject("ADODB.Recordset")
rsnews.ActiveConnection = MM_conn_STRING
rsnews.Source = "SELECT * FROM dbo.zooNews WHERE nisshow = 1 ORDER BY naddtime DESC"
rsnews.CursorType = 1
rsnews.CursorLocation = 2
rsnews.LockType = 1
rsnews.Open()
rsnews_numRows = 0
%>
<% '使用重复行为(重复下面的<tr>标签内容)
Dim Repeat2__numRows
Dim Repeat2__index
Repeat2__numRows = 20
Repeat2__index = 0
rsnews_numRows = rsnews_numRows + Repeat2__numRows
%>
<html>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="40" align="right">
<% '关键在这里,在重复内容上面产生一个导航条
dim RowCount
RowCount = 20
call TurnPage(rsnews,RowCount)
%>
</td>
</tr>
<tr>
<td>
<table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="1" valign="top" class="dotX"><img src="http://study.zhupao.com//Files/BeyondPic/062220437321525.gif" width="1" height="1"></td>
</tr>
<% '重复开始
While ((Repeat2__numRows <> 0) AND (NOT rsnews.EOF))
%>
<tr>
<td height="25"><% =(rsnews.Fields.Item("ntitle").Value) %>
<input type="submit" name="Submit" value="Submit">
</td>
</tr>
<%
Repeat2__index=Repeat2__index+1
Repeat2__numRows=Repeat2__numRows-1
rsnews.MoveNext()
Wend '重复结束
%>
<tr>
<td height="1" valign="top" class="dotX"><img src="http://study.zhupao.com//Files/BeyondPic/062220437321525.gif" width="1" height="1"></td>
</tr>
</table></td>
</tr>
<tr>
<td height="40" align="right">
<% '在重复内容下面产生一个导航条,可选,注意这里不再需要dim RowCount
RowCount = 20
call TurnPage(rsnews,RowCount)
%> </td>
</tr>
</table>
</body>
</html>
<% '关闭记录集
rsnews.Close()
Set rsnews = Nothing
%>
<!--#i nclude file="Connections/conn.asp" -->
<!--#i nclude file="pagein.asp"--> '引入公共翻页模块
<% '定义记录集
Dim rsnews
Dim rsnews_numRows
Set rsnews = Server.CreateObject("ADODB.Recordset")
rsnews.ActiveConnection = MM_conn_STRING
rsnews.Source = "SELECT * FROM dbo.zooNews WHERE nisshow = 1 ORDER BY naddtime DESC"
rsnews.CursorType = 1
rsnews.CursorLocation = 2
rsnews.LockType = 1
rsnews.Open()
rsnews_numRows = 0
%>
<% '使用重复行为(重复下面的<tr>标签内容)
Dim Repeat2__numRows
Dim Repeat2__index
Repeat2__numRows = 20
Repeat2__index = 0
rsnews_numRows = rsnews_numRows + Repeat2__numRows
%>
<html>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="40" align="right">
<% '关键在这里,在重复内容上面产生一个导航条
dim RowCount
RowCount = 20
call TurnPage(rsnews,RowCount)
%>
</td>
</tr>
<tr>
<td>
<table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="1" valign="top" class="dotX"><img src="http://study.zhupao.com//Files/BeyondPic/062220437321525.gif" width="1" height="1"></td>
</tr>
<% '重复开始
While ((Repeat2__numRows <> 0) AND (NOT rsnews.EOF))
%>
<tr>
<td height="25"><% =(rsnews.Fields.Item("ntitle").Value) %>
<input type="submit" name="Submit" value="Submit">
</td>
</tr>
<%
Repeat2__index=Repeat2__index+1
Repeat2__numRows=Repeat2__numRows-1
rsnews.MoveNext()
Wend '重复结束
%>
<tr>
<td height="1" valign="top" class="dotX"><img src="http://study.zhupao.com//Files/BeyondPic/062220437321525.gif" width="1" height="1"></td>
</tr>
</table></td>
</tr>
<tr>
<td height="40" align="right">
<% '在重复内容下面产生一个导航条,可选,注意这里不再需要dim RowCount
RowCount = 20
call TurnPage(rsnews,RowCount)
%> </td>
</tr>
</table>
</body>
</html>
<% '关闭记录集
rsnews.Close()
Set rsnews = Nothing
%>