DIV CSS 佈局教程網

Dreamweaver與ASP實現分頁技術的實作參考
編輯:Dreamweaver相關     

今天想跟用 DW + ASP 製作網頁的朋友們,分享一份分頁技術的實作參考,一起告別只有「第一頁、前一頁、下一頁、最後一頁」那種受限的翻頁體驗。最終呈現的效果,是形如「第 N 頁〔共 * 頁〕 << 1 2 3 4 5 6 7 8 9 10 >>」的數字頁碼導航。

觀察Dreamweaver自動產生的代碼

用 DW + ASP 製作網頁,在綁定記錄集後,代碼頁會立刻出現以下內容:

<%
Dim Recordset1
Dim Recordset1_numRows
Set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_數據庫名_STRING
Recordset1.Source = "SELECT * FROM 表名"
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 1
Recordset1.Open()
Recordset1_numRows = 0
%>

改寫為支援分頁的版本

接著把上述代碼改寫成支援分頁的形式。關鍵在於宣告分頁變數、把 CursorType 由 0 改為 1,並加入 PageSize 與 AbsolutePage:

<%
Dim I
Dim RPP
Dim PageNo
I=1
RPP=50
PageNo=CInt(Request("PageNo"))
' 以上為新插入的
Dim Recordset1
Dim Recordset1_numRows
Set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_數據庫名_STRING
Recordset1.Source = "SELECT * FROM 數據庫名"
Recordset1.CursorType = 1 ' 將上面的 0 改為 1
Recordset1.CursorLocation = 2
Recordset1.LockType = 1
Recordset1.Open()
Recordset1_numRows = 0 ' 於此行下方加入:
Recordset1.PageSize=RPP
If PageNo<=0 Then PageNo=1
If PageNo>Recordset1.PageCount Then PageNo=Recordset1.PageCount
Recordset1.AbsolutePage=PageNo

加入頁碼與導航副程式

然後定義 ShowPageInfo 與 ShowPageNavi 兩個 Sub:前者輸出「第 N 頁〔共 * 頁〕」,後者產生數字頁碼與前後翻頁連結,NaviLength 用來決定一次顯示幾個數字:

Sub ShowPageInfo(tPageCount,cPageNo)
Response.Write "第"&cPageNo&"頁[共"&tPageCount&"頁]"
End Sub
Sub ShowPageNavi(tPageCount,cPageNo)
If cPageNo<1 Then cPageNo=1
If tPageCount<1 Then tPageCount=1
If cPageNo>tPageCount Then cPageNo=tPageCount
Dim NaviLength
NaviLength=10 ' 顯示的數字連結個數
Dim I,StartPage,EndPage
StartPage=(cPageNo\NaviLength)*NaviLength+1
If (cPageNo Mod NaviLength)=0 Then StartPage=StartPage-NaviLength
EndPage=StartPage+NaviLength-1
If EndPage>tPageCount Then EndPage=tPageCount
……(後續以迴圈輸出各頁碼連結)……
End Sub

透過 Recordset 的 PageSize、PageCount 與 AbsolutePage 這組屬性,ASP 便能將龐大的記錄切成若干頁;再搭配自訂的導航副程式,就能靈活地呈現數字分頁,徹底擺脫四個固定按鈕的侷限。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved