晚上,应一位网友的求助,偶写了个最简单的查询带分页功能的页面(跳转分页时查询条件不丢失),希望对大家有所帮助.
先建立一个Access的数据库Db1.mdb,里面有一个数据表Dicky,共4个字段:
字段名 数据类型
ID AutoNumber
Name Text
Sex Text
Age Number
QQ Number
建立完库后请自行添加几条数据作为测试.
Page.asp代码如下:
<html>
<head>
<title>分页测试</title>
<script language="javascript">
<!--
//分頁跳轉函數
function GotoPageSubmit(GotoPage)
{
form1.GotoPage.value = GotoPage;
form2.GotoPage.value = GotoPage;
form1.submit();
}
//-->
</script>
</head>
<body>
<table align=center>
<form name=form1 method=post>
<input type=hidden name=GotoPage>
<tr><td>
<input type=checkbox name=Sex value="男"<%If Instr(Replace(Request("Sex")," ",""),"男") <> 0 Then Response.Write " checked"%>>男<input type=checkbox name=Sex value="女"<%If Instr(Replace(Request("Sex")," ",""),"女") <> 0 Then Response.Write " checked"%>>女
<input type=submit name=search value=搜索>
</td>
</tr>
</form>
</table>
<table width=100% align=center>
<%
Dim Db,ConnStr,Conn,Rs,Sql
Set Conn = Server.CreateObject("ADODB.Connection")
Db = "db1.mdb"
ConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath(Db)
Conn.Open ConnStr
If Trim(Request("Sex")) = "" Or Trim(Request("Sex")) = "男, 女" Then
Sql = "Select * From Dicky"
Else
Sql = "Select * From Dicky Where Sex='"&Trim(Request("Sex"))&"'"
End If
'Response.Write Sql
Set Rs = Server.CreateObject("Adodb.RecordSet")
Rs.Open Sql,Conn,1,1
'Set Rs = Conn.Execute(Sql)
If Rs.Eof Then
Response.Write "<tr align=center><td>对不起,尚无记录!</td></tr>"
Else%>
<tr><th>姓名</th><th>性别</th><th>QQ</th></tr>
<% '*************分頁處理部分By Dicky 2004-12-10 10:49:32**************
Dim Page,Page_Number,TotalRec,MaxPerPage,MM
'Page(當前頁),Page_Number(縂頁數),TotalRec(縂記錄數),MaxPerPage(每頁顯示條數)
MM = 0
If Trim(Request.Form("GotoPage")) <> "" Then
If Not IsNumeric(Trim(Request.Form("GotoPage"))) Then
Page = 1
Else
Page = CLng(Trim(Request.Form("GotoPage")))
End If
Else
Page = 1
End If
Rs.PageSize = 1
MaxPerPage = Rs.pageSize
TotalRec = Rs.RecordCount
Page_Number = Rs.PageCount
If Page_Number = 0 Then Page_Number = 1
If CLng(Page) < 1 Then Page = 1
If Page > Page_Number Then Page = Page_Number
Rs.AbsolutePage = Page
'*************分頁處理部分By Dicky 2004-12-10 10:49:32**************
Do While Not Rs.Eof And MM < MaxPerPage
Response.Write "<tr align=center><td>"&Rs("Name")&"</td><td>"&Rs("Sex")&"</td><td>"&Rs("QQ")&"</td></tr>"
MM = MM + 1
Rs.MoveNext:Loop
End If
Rs.Close
Set Rs = Nothing
Conn.Close
Set Conn = Nothing
%>
</table>
<!-- 分頁公用文件 By Dicky 2004-12-10 11:01:26 -->
<%
'If Page_Number > 1 Then%>
<table width="100%" style="font-size: 9pt; margin-top: 4px">
<form method="post" name="form2">
<tr align="center"><td>
<%
Response.Write "共<b><font color=red>"&TotalRec&"</font></b>条"
Response.Write " 页次:<b><font color=red>"&Page&"</font></b>/<b>"&Page_Number&"</b>页"
Response.Write " 每页<b><font color=red>"&MaxPerPage&"</font></b>条"
%>
分页:<% If Page = 1 Then
Response.Write "[首页]"
Response.Write "[上一页]"
Else
Response.Write "[<a href='javascript:GotoPageSubmit(1);'><font color=blue>首页</font></a>]"
Response.Write "[<a href='javascript:GotoPageSubmit("&Page-1&");'><font color=blue>上一页</font></a>]"
End If
If Page_Number - Page >= 1 Then
Response.Write "[<a href='javascript:GotoPageSubmit("&Page+1&");'><font color=blue>下一页</font></a>]"
Response.Write "[<a href='javascript:GotoPageSubmit("&Page_Number&");'><font color=blue>尾页</font></a>]"
Else
Response.Write "[下一页]"
Response.Write "[尾页]"
End If%>
转到第 <input name="GotoPage" type=text size="3" maxlength="3" style="text-align:center;color:#FF0000;" value="<%=Page%>" οnmοuseοver="this.select();"> 页 <input name=goto type=button onClick="javascript:GotoPageSubmit(form2.GotoPage.value);" value=" Go ">
</td></tr>
</form>
</table>
<%
'End If%>
</body>
</html>