最簡單查詢帶分頁功能的代碼

來源:互聯網
上載者:User

晚上,應一位網友的求助,偶寫了個最簡單的查詢帶分頁功能的頁面(跳轉分頁時查詢條件不丟失),希望對大家有所協助.
先建立一個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%>" onmouseover="this.select();"> 頁&nbsp;<input name=goto type=button onClick="javascript:GotoPageSubmit(form2.GotoPage.value);" value=" Go ">
</td></tr>
</form>
</table>
<%
'End If%>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.