用ASP實現分頁的源碼

來源:互聯網
上載者:User
分頁 <%''本程式檔案名稱為:Pages.asp%>
<%''包含ADO常量表檔案adovbs.inc,可從"\Program Files\Common Files\System\ADO"目錄下拷貝%>
<!--#Include File="adovbs.inc"-->
<%''*建立資料庫連接,這裡是Oracle8.05資料庫
Set conn=Server.CreateObject("ADODB.Connection") 
conn.Open "Provider=msdaora.1;Data Source=YourSrcName;User ID=YourUserID;Password=YourPassword;" 

Set rs=Server.CreateObject("ADODB.Recordset")  ''建立Recordset對象
rs.CursorLocation=adUseClient          ''設定記錄集指標屬性
''*設定一頁內的記錄總數,可根據需要進行調整
rs.PageSize=10                  

''*設定查詢語句  
StrSQL="Select ID,姓名,住址,電話 from 通訊錄 Order By ID"    
rs.Open StrSQL,conn,adOpenStatic,adLockReadOnly,adCmdText
%>
<HTML>
<HEAD>
<title>分頁樣本</title>
<script language=javascript>
//點擊"[第一頁]"時響應:
function PageFirst()
{
  document.MyForm.CurrentPage.selectedIndex=0;
  document.MyForm.CurrentPage.onchange();
}
//點擊"[上一頁]"時響應:
function PagePrior()
{  
  document.MyForm.CurrentPage.selectedIndex--;
  document.MyForm.CurrentPage.onchange();
}
//點擊"[下一頁]"時響應:
function PageNext()
{
  document.MyForm.CurrentPage.selectedIndex++;
  document.MyForm.CurrentPage.onchange();    
}
//點擊"[最後一頁]"時響應:
function PageLast()
{ 
  document.MyForm.CurrentPage.selectedIndex=document.MyForm.CurrentPage.length-1;
  document.MyForm.CurrentPage.onchange();
}
//選擇"第?頁"時響應:
function PageCurrent()
{ //Pages.asp是本程式的檔案名稱
  document.MyForm.action='Pages.asp?Page='+(document.MyForm.CurrentPage.selectedIndex+1)
  document.MyForm.submit();
} 
</Script>
</HEAD>
<BODY bgcolor="#ffffcc" link="#008000" vlink="#008000" alink="#FF0000"">
 
<%IF rs.Eof THEN
  Response.Write("<font size=2 color=#000080>[資料庫中沒有記錄!]</font>")
ELSE 
  ''指定當前頁碼
  If Request("CurrentPage")="" Then
   rs.AbsolutePage=1
  Else
   rs.AbsolutePage=CLng(Request("CurrentPage"))
  End If 
 
  ''建立表單MyForm,方法為Get
  Response.Write("<form method=Get name=MyForm>") 
  Response.Write("<p align=center><font size=2 color=#008000>")
  ''設定翻頁超連結
  if rs.PageCount=1 then 
   Response.Write("[第一頁] [上一頁] [下一頁] [最後一頁] ")
  else
    if rs.AbsolutePage=1 then
     Response.Write("[第一頁] [上一頁] ")
     Response.Write("[<a href=javascript:PageNext()>下一頁</a>] ")
     Response.Write("[<a href=javascript:PageLast()>最後一頁</a>] ")
    else
      if rs.AbsolutePage=rs.PageCount then
       Response.Write("[<a href=javascript:PageFirst()>第一頁</a>] ")
       Response.Write("[<a href=javascript:PagePrior()>上一頁</a>] ")
       Response.Write("[下一頁] [最後一頁] ")
      else
        Response.Write("[<a href=javascript:PageFirst()>第一頁</a>] ")
        Response.Write("[<a href=javascript:PagePrior()>上一頁</a>] ")
        Response.Write("[<a href=javascript:PageNext()>下一頁</a>] ")
        Response.Write("[<a href=javascript:PageLast()>最後一頁</a>] ")
      end if
    end if
  end if
 
  ''建立下拉式清單方塊,用於選擇瀏覽頁碼
  Response.Write("第<select size=1 name=CurrentPage onchange=PageCurrent()>")  
  For i=1 to rs.PageCount
   if rs.AbsolutePage=i then
    Response.Write("<option selected>"&i&"</option>") ''當前頁碼
   else
    Response.Write("<option>"&i&"</option>")
   end if 
  Next
  Response.Write("</select>頁/共"&rs.PageCount&"頁 共"&rs.RecordCount&"條記錄</font><p>")
  Response.Write("</form>")
 
  ''建立表格,用於顯示
  Response.Write("<table align=center cellspacing=1 cellpadding=1 border=1")  
  Response.Write(" bordercolor=#99CCFF bordercolordark=#b0e0e6 bordercolorlight=#000066>")
 
  Response.Write("<tr bgcolor=#ccccff bordercolor=#000066>")
  
  Set Columns=rs.Fields
 
  ''顯示表頭
  For i=0 to Columns.Count-1
   Response.Write("<td align=center width=200 height=13>")
   Response.Write("<font size=2><b>"&Columns(i).name&"</b></font></td>") 
  Next
  Response.Write("</tr>")
  ''顯示內容
  For i=1 to rs.PageSize
   Response.Write("<tr bgcolor=#99ccff bordercolor=#000066>")
   For j=0 to Columns.Count-1
    Response.Write("<td><font size=2>"&Columns(j)&"</font></td>")
   Next
   Response.Write("</tr>")
  
   rs.movenext
   if rs.EOF then exit for
  Next
 
  Response.Write("</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.