| 这个分页代码使用很简单,只有4个步骤: (1)构造 SQL 查询语句、设置数据库连接字符串、设置每页记录显示数目 (2)插入分页控制代码 navigator_init.inc 文件 (3)在页面的适当地方(即你想显示“上页、下页”的地方),插入分页导航代码 navigator.inc (4)用循环显示本页记录
假定我们的网站目录结构如下:
asp/' ASP 文件路径 images/' 图形文件路径 inc/' 包含文件路径 content/' 存放诸如 header.inc、footer.inc 等每个页面都需要包含的文件 control/' 存放诸如 navigator.inc、query.inc 等于分页控制有关的文件
额外的建议: 请将本页面的内容复制到 EditPlus 2.1 中,然后阅读(以后的文章均照此处理),您将会觉得阅读起来非常轻松,因为它的颜色配置非常舒服。当然我强烈推荐您也用 EditPlus 2.1 来编辑 .htm、.asp 、.inc 文件。
<一> 需要分页的 ASP 文件
sample.asp
<% ' 取得当前 ASP 页面的完整路径,重要技巧 theScript= Request.ServerVariables("SCRIPT_NAME") myconnstr= "driver={SQL Server};server=yourserver;uid=sa;pwd=;database=mytest" thePageSize= 20' 每页显示的记录数
' (1)构造 SQL 语句
uSQL= "select * from yourtablename "
' (2)插入分页控制代码 %> <!--#include file="../inc/control/navigator_init.inc"-->
<html> <head> <title>test</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <link rel="stylesheet" href="../default.css" type="text/css"> </head>
<!-- 你的 HTML 代码//-->
<table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td> <!--(3)插入分页导航代码--> <!--#include file="../inc/control/navigator.inc"--> </td> </tr>
<!-- 你的 HTML 代码//-->
<table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td> <!--(4)用循环显示本页记录-->
<% For i = 1 To rs.pagesize %> <!-- 你的记录显示代码//--> <% rs.MoveNext If rs.EOF Then Exit For rs.close conn.close End If next %>
</td> </tr>
<!-- 你的 HTML 代码//-->
</body> </html>
<二> 分页控制代码:
navigator_init.inc
<% theCurrentPageNum= clng(request("pn"))
' 用页面名字来唯一确定当前页面使用的 rs ' 由于不同目录下面可以有相同名字的文件,因此使用带完整路径的文件名 theCurrentRS= "rs" & theScript
If theCurrentPageNum = 0 Then
Set conn = Server.CreateObject("ADODB.Connection") conn.open myconnstr set rs = server.createobject("adodb.recordset") rs.open uSQL,conn,1,3
if rs.recordcount > 0 then rs.PageSize= thePageSize rs.AbsolutePage= 1 theCurrentPageNum= 1 Set Session(theCurrentRS) = rs end if
else Set rs = Session(theCurrentRS) if rs.recordcount > 0 then rs.AbsolutePage= theCurrentPageNum end if End If
'构造导航条
href_first= theScript & "?mo=0&pn=1" href_prev= theScript & "?mo=0&pn=" & (theCurrentPageNum-1) href_next= theScript & "?mo=0&pn=" & (theCurrentPageNum+1) href_last= theScript & "?mo=0&pn=" & rs.pagecount
strnav_first= "<a href=" & href_first & ">首页</a>" strnav_prev= "<a href=" & href_prev & ">前页</a>" strnav_next= "<a href=" & href_next & ">后页</a>" strnav_last= "<a href=" & href_last & ">末页</a>"
if rs.pagecount > 1 then '在首页 if theCurrentPageNum = 1 then strnav_first= "首页" strnav_prev = "前页" end if '在末页 if theCurrentPageNum = rs.pagecount then strnav_next = "后页" strnav_last = "末页" end if else strnav_first= "首页" strnav_prev = "前页" strnav_next = "后页" strnav_last = "末页" end if
strnav = strnav_first & " " & strnav_prev & " " & strnav_next & " " & strnav_last
' 由于在 listrecords 函数中,当显示完所有记录时将关闭 rs ,因此先把 rs.recordcount 保存起来 theRsCount = rs.recordcount %>
<三> 分页导航代码:
navigator.inc
注意 form 表单一定要有自己的名字,例如 form_nav ,以和其他表单区分
<table width="300" border="0" align="right" cellpadding="0" cellspacing="0" height="30"> <form action="<%=theScript%>" method="post" name="form_nav" onsubmit="javascript:return checkpn(this);"> <tr> <td align="right"> <%=rs.recordcount%> 条 <%=rs.pagecount%>页 第<input type="text" name="pn" value="<%=theCurrentPageNum%>" size=2 maxlength=4 class="form_text_underline">页 <%= strnav %> </td> </tr> </form>
<script language="javascript">
// 判断用户输入的页码是否合法
function checkpn(form) { pn = form.pn ;
if (pn.value=="") { alert("请输入页码") ;pn.select();return false;}
if (!uisNum(pn.value)) { alert("页码是数字") ;pn.select();return false;}
if (pn.value-<%=rs.pagecount%>>0) { alert("页码不能大于<%=rs.pagecount%>") ;pn.select();return false;}
return true; }
function aLength(str) { var i,len; len=0; for(i=0;i<str.length;i++) { if(str.charCodeAt(i) > 255) len++; len++; } return len; }
function trimString(str) { var i,j;
if(str == "") return "";
for(i=0;i<str.length;i++) if(str.charAt(i) != ' ') break; if(i >= str.length) return "";
for(j=str.length-1;j>=0;j--) if(str.charAt(j) != ' ') break;
return str.substring(i,j+1); }
function uisNum(str) { var i, cChar;
str = trimString(str);
for(i=0;i<str.length;i++) { cChar = str.charAt(i); if( !(("0"<=cChar) && (cChar<="9")) ) return false; } return true; }
</script>
|