日期:2011-10-18  浏览次数:21369 次

用DWMX2004的插件包可以实现ASP的各种分页功能,但是那些代码实在有点恐怖,我们在实际的项目开发中,有没有更简单的方法实现"一劳永逸"呢?答案是肯定的.

我们需要的功能:

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

具体编写内容



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

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


使用范例:

1.

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

<%@LANGUAGE="VBSCRIPT"CODEPAGE="936"%>
<!--#includefile="Connections/conn.asp"-->
<!--#includefile="pagein.asp"-->'引入公共翻页模块
<%'定义记录集
Dimrsnews
Dimrsnews_numRows

Setrsnews=Server.CreateObject("ADODB.Recordset")
rsnews.ActiveConnection=MM_conn_STRING
rsnews.Source="SELECT*FROMdbo.zooNewsWHEREnisshow=1ORDERBYnaddtimeDESC"
rsnews.CursorType=1
rsnews.CursorLocation=2
rsnews.LockType=1
rsnews.Open()

rsnews_numRows=0
%>
<%'使用重复行为(重复下面的<tr>标签内容)
DimRepeat2__numRows
DimRepeat2__index

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

<html>
<tablewidth="100%"border="0"cellspacing="0"cellpadding="0">
<tr>
<tdheight="40"align="right">
<%'关键在这里,在重复内容上面产生一个导航条
dimRowCount
RowCount=20
callTurnPage(rsnews,RowCount)
%>
</td>
</tr>
<tr>
<td>
<tablewidth="95%"border="0"align="center"cellpadding="0"cellspacing="0">
<tr>
<tdheight="1"valign="top"class="dotX"><imgsrc="images/spacer.gif"width="1"height="1"></td>
</tr>
<%'重复开始
While((Repeat2__numRows<>0)AND(NOTrsnews.EOF))
%>
<tr>
<tdheight="25"><%=(rsnews.Fields.Item("ntitle").Value)%>
<inputtype="submit"name="Submit"value="Submit">
</td>
</tr>
<%
Repeat2__index=Repeat2__index+1
Repeat2__numRows=Repeat2__numRows-1
rsnews.MoveNext()
Wend'重复结束
%>
<tr>
<tdheight="1"valign="top"class="dotX"><imgsrc="images/spacer.gif"width="1"height="1"></td>
</tr>
</table></td>
</tr>
<tr>
<tdheight="40"align="right">
<%'在重复内容下面产生一个导航条,可选,注意这里不再需要dimRowCount
RowCount=20
callTurnPage(rsnews,RowCount)
%></td>
</tr>
</table>
</body>
</html>

<%'关闭记录集
rsnews.Close()
Setrsnews=Nothing
%>


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