如何快捷地实现分页显示功能?
来源:asp之家 时间:2010-01-01 15:08:00
也许是这样的。下面我们来推荐一个简单的分页程序,代码和说明见下(两段虚线“-----”间的代码是实现该功能的重要语句):
chunfeng.asp
' 分页
<%
----------------------------------------------------------------------------------------------------------------------
theScript = Request.ServerVariables("SCRIPT_NAME")
' 取得当前ASP页面的完整路径
myconnstr = "driver={SQL Server};server=yourserver;uid=sa;pwd=;database=yourdatabase"
' 构造 SQL 语句
thePageSize = 20
' 每页显示的记录数
uSQL = "select * from yourtablename "
----------------------------------------------------------------------------------------------------------------------
%>
----------------------------------------------------------------------------------------------------------------------
<!--#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>
----------------------------------------------------------------------------------------------------------------------
<!--#include file="../inc/control/navigator.inc"-->
<!--插入分页导航代码-->
----------------------------------------------------------------------------------------------------------------------
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
----------------------------------------------------------------------------------------------------------------------
<% For i = 1 To rs.pagesize %>
<!—用循环显示本页记录-->
----------------------------------------------------------------------------------------------------------------------
<!-- 记录显示代码//-->
<%
rs.MoveNext
If rs.EOF Then
Exit For
rs.close
conn.close
End If
next
%>
</td>
</tr>
</table>
</body>
</html>
navigator_init.inc
' 分页控制代码
<%
theCurrentPageNum = clng(request("pn"))
theCurrentRS = "rs" & theScript
' 用页面名字来唯一确定当前一页面使用的 rs,因为不同目录下面可能有名字相同的文件,因此必须使用带完整路径的文件名
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
theRsCount = rs.recordcount
' 由于在 listrecords 函数中,显示完所有记录时将关闭rs,因此要先把rs.recordcount保存起来
%>
navigator.inc
' 分页导航
<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>
</table>
<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>