先將分頁類別原始碼發上來,接著有空會發一些實例來講解其用法,以及如何擴充其功能。
類別原程式碼如下:
< %
'==============================================
'XDOWNPAGE ASP版本
'目前版本:2.0
'
'
'原版本 1.00
'Code by zykj2000
'Email: [email protected]
'BBS: http://bbs.513soft.net
'
'
'升級版本:1.5 (asp + oracle)
updated by doublel
Email:
blog: ttp://blog.csdn.net/doublel/
'升級說明:
'
'
'升級版本:2.0 ' (asp + oracle) ---->目前版本
'保留原名:XDOWNPAGE
Updated by northsnow
'email: [email protected]
'blog: http://blog.csdn.net/precipitant
'升級說明:
'1 , 資料查詢時只查詢目前頁所包含的記錄,大幅降低了資料傳輸量
'2 , 如果正常的頁導航,不用每次都查詢總記錄數,只要第一次查詢後,後來透過參數傳遞即可
'3 , 支援動態變更頁大小
'4 , 支援動態排序
'5 , 本程式只支援oracle,如果想用在sqlserver或其他類型的資料庫,請自行修改:Public Property Let GetSQL(str_sql)即可。
'
'
'其他程式修改者訊息,請在原始碼中查對! ! !
'本程式可以免費使用、修改、複製、轉載、引用,希望我的程式能為您的工作帶來方便
'但請保留以上請息,特別是原著信息。另外如果作為商業用途,請與原著和該版本升級
'人聯繫以取得許可。
'
'
'程序特點
'本程式主要是對資料分頁的部分進行了封裝,而資料顯示部分完全由使用者自定義,
'支援URL多個參數
'
'使用說明
'程式參數說明
'PapgeSize 定義分頁每一頁的記錄數
'GetRS 傳回經過分頁的Recordset此屬性唯讀
'GetConn 得到資料庫連接
'GetSQL 得到查詢語句
'totalRecordCount 傳遞總記錄數
'程序方法說明
'ShowPage 顯示分頁導覽條,唯一的公用方法
'ShowPageSizeChange() 顯示改變頁大小的列表
'
'例:
'
' '包含文件
'
' Set mypage=new xdownpage '建立對象
' mypage.getconn=conn '得到資料庫連接
' mypage.getsql="select * from productinfo order by id asc"
' mypage.pagesize=5 '設定每一頁的記錄條資料為5條
'mypage.totalRecordCount=rsTotalCount 設定總記錄數
' set rs=mypage.getrs() '返回Recordset
'mypage.GetSubmitForm="frmQuery" ' 分頁預設提交的表單,currentpage參數
'Response.write(mypage.GetSubmitForm1()) '輸出分頁提交的函數
' mypage.showpage() '顯示分頁訊息,這個方法可以,在set rs=mypage.getrs()以後
' 任意位置調用,可以調用多次
' do while not rs.eof '接下來的操作就和操作一個普通Recordset物件一樣操作
' response.write rs(0) & "
' " '這裡就可以自訂顯示方式了
' rs.movenext
' loop
'
'新增了保存目前頁面數量的提交腳本
'函數為GetSubmitForm()
'需要提交給函數GetSubmitForm一個表單名字
'在這個提交的表單裡面保存變數flag,currentpage,pagesize,rsTotalCount 四個參數
'例子如下
'flag=request("flag")
'currentpage=request("currentpage")
'currentpage=request("pagesize")
'currentpage=request("rsTotalCount")
'在提交的表單裡面加入下面四個input
'<input name="flag" type="hidden" value="< % =flag% >">
'<input name="currentpage" type="hidden" value="< % =currentpage% >">
'<input name="pagesize" type="hidden" value="< % =pagesize% >">
'<input name="rsTotalCount" type="hidden" value="< % =rsTotalCount% >">
'==============================================
Const Btn_First= "<font face=""webdings"">9</font>" '定義第一頁按鈕顯示樣式
Const Btn_Prev="<font face=""webdings"">3</font>" '定義前一頁按鈕顯示樣式
Const Btn_Next="<font face=""webdings"">4</font>" '定義下一頁按鈕顯示樣式
Const Btn_Last="<font face=""webdings"">:</font>" '定義最後一頁按鈕顯示樣式
Const XD_Align="center" '定義分頁資訊對齊方式
Const XD_Width="100%" '定義分頁資訊框大小
Const XD_Height="20"
Class Xdownpage '類別從這裡開始
'變數定義
public int_totalPage '總頁數
public int_curcount '目前頁的記錄數
public XD_PageSize '頁大小
Private int_curpage '目前頁號
Private int_totalRecord '總記錄數
Private XD_Conn '資料庫連線對象
Private XD_Rs '記錄集對象
Private XD_SQL '主sql語句
Private XD_Count_SQL '查詢總記錄數的sql語句
Private Str_errors
Private str_URL
Private XD_sURL
Private SubmitForm '所需的查詢表單名字(隱藏表單名字)
'================================================== ==================
'PageSize 屬性
'設定每一頁的分頁大小
'================================================== ==================
Public Property Let PageSize(int_PageSize)
If IsNumeric(Int_Pagesize) Then
if clng(Int_Pagesize)>0 then
XD_PageSize=CLng(int_PageSize)
else
XD_PageSize=10
end if
Else
XD_PageSize=10
End If
End Property
Public Property Get PageSize
If XD_PageSize="" or (not(IsNumeric(XD_PageSize))) Then
PageSize=10
Else
PageSize=XD_PageSize
End If
End Property
'================================================== ==================
'GetRS 屬性
'返回分頁後的記錄集
'================================================== ==================
Public Property Get GetRs()
Set XD_Rs=Server.createobject("adodb.recordset")
'XD_Rs.PageSize=PageSize
XD_Rs.CursorLocation=3
XD_Rs.Open XD_SQL,XD_Conn,3,1
int_curcount=XD_Rs.RecordCount
if int_totalRecord="" or not isNumeric(int_totalRecord) then int_totalRecord=0 '規範化int_totalRecord的值
if int_totalRecord=0 and (int_curcount>=PageSize or int_curpage>1) then call queryRsCount() '查詢總記錄數
if err.number<>0 then
Response.Write err.Clear
end if
Set GetRs=XD_RS
End Property
'================================================= ==================
'queryRSCount 方法
'查詢總記錄數
'================================================== ==================
Public sub queryRsCount()
'下面程式碼用於計算總記錄數
if XD_Count_SQL<>"" then
set rs_sqlcount=server.createobject("adodb.recordset")
rs_sqlcount.CursorLocation=3
rs_sqlcount.open XD_Count_SQL,conn,3,1
if (rs_sqlcount.eof and rs_sqlcount.bof) then
int_totalRecord=0
else
int_totalRecord=rs_sqlcount(0)
int_totalRecord=clng(int_totalRecord)
end if
rs_sqlcount.close
set rs_sqlcount=nothing
end if
描述 sub
'================================================= ===================
'GetConn 得到資料庫連接
'
'================================================== ===============
Public Property Let GetConn(obj_Conn)
Set XD_Conn=obj_Conn
End Property
'================================================= ===================
'GetSQL 得到查詢語句
'
'================================================== ===============
Public Property Let GetSQL(str_sql)
if (str_sql<>"") then
'根據給定查詢語句,產生最終的查詢語句(只取當前頁內容):適用於oracle資料庫
XD_SQL=" select * from (select rownum r_n,temptable.* from ("
XD_SQL=XD_SQL&str_sql
XD_SQL=XD_SQL&" ) temptable ) where r_n between " & cstr((int_curpage -1) * XD_PageSize +1) & " and " & cstr(int_curpage * XD_PageSize)
'查詢總記錄數的查詢語句
XD_Count_SQL="select count(*) from ("& str_sql & ")"
end if
End Property
'================================================== ===============
'GetSubmitForm屬性 設定查詢條件的表單
'
'================================================== ===============
Public Property Let GetSubmitForm(frmName)
SubmitForm=trim(frmName)
End Property
'================================================= ===================
'GetSubmitForm1方法 輸出分頁導覽所需腳本
'
'================================================== ===============
public sub GetSubmitForm1()
'頁導航的javascript函數
Response.Write " "+vrcrlf
Response.Write ("<Script language=""javascript"">") +vbcrlf
Response.Write " function generalSubmit(i)"+vbcrlf
Response.Write " {"+vbcrlf
Response.Write " document."&SubmitForm&".flag.value=""query1111111155555"";"+vbcrlf
Response.Write " document."&SubmitForm&".currentpage.value=i;"+vbcrlf
Response.Write " "&SubmitForm&".submit();"+vbcrlf
Response.Write " }"+vbcrlf
'改變頁大小的javascript函數
Response.Write " function changePageSize(ii)"+vbcrlf
Response.Write " {"+vbcrlf
Response.Write " document."&SubmitForm&".flag.value=""query1111111155555"";"+vbcrlf
Response.Write " document."&SubmitForm&".currentpage.value=1;"+vbcrlf
Response.Write " document."&SubmitForm&".pagesize.value=ii;"+vbcrlf
Response.Write " "&SubmitForm&".submit();"+vbcrlf
Response.Write " }"+vbcrlf
Response.Write ("</Script>")+vbcrlf
Response.Write " "+vrcrlf
end sub
'================================================= =====================
'totalRecordCount 屬性
'關於記錄總數
'
'================================================== =================
Public Property Let totalRecordCount(int_totalRecordCount)
If IsNumeric(int_totalRecordCount) Then
int_totalRecord=CLng(int_totalRecordCount)
End If
End Property
Public Property Get totalRecordCount
If not(int_totalRecord="" or (not(IsNumeric(int_totalRecord)))) Then
totalRecordCount=int_totalRecord
End If
End Property
'================================================== ===================
'GetRecordCount 方法
'傳回目前記錄數
'
'================================================== ===================
public function GetRecordCount()
GetRecordCount=int_totalRecord
end function
'================================================== ===================
'Class_Initialize 類別的初始化
'初始化當前頁的值
'
'================================================== ===================
Private Sub Class_Initialize
'==========================
'設定一些參數的黙認值
'==========================
' XD_PageSize=10 '設定分頁的預設值為10
'==========================
'取得當前面的值
'==========================
If Request("currentpage")="" Then
int_curpage=1
ElseIf not(IsNumeric(Request("currentpage"))) Then
int_curpage=1
ElseIf CInt(Trim(Request("currentpage")))<1 Then
int_curpage=1
Else
Int_curpage=CInt(Trim(Request("currentpage")))
End If
End Sub
'===============================================
'ShowPage 建立分頁導覽條
'有首頁、前一頁、下一頁、末頁、還有數位導航
'
'==============================================
Public Sub ShowPage()
Dim str_tmp
XD_sURL = GetUrl()
' int_totalRecord=XD_Rs.RecordCount
If int_totalRecord<=0 Then
str_error=str_error & "總記錄數為零,請輸入資料"
Call ShowError()
End If
If int_totalRecord="" then
int_TotalPage=1
Else
'modify by wls 041215 For the right pages display---------------
If int_totalRecord mod PageSize =0 Then
int_TotalPage = CLng(int_TotalRecord XD_PageSize * -1)*-1
Else
int_TotalPage = CLng(int_TotalRecord XD_PageSize * -1)*-1+1
End If
End If
If Int_curpage>int_Totalpage Then
int_curpage=int_TotalPage
End If
'================================================= =========
'顯示分頁訊息,各模組依自行要求變更顯求位置
'================================================== =======
'response.write " "
str_tmp=ShowFirstPrv
response.write str_tmp
str_tmp=showNumBtn
response.write str_tmp
str_tmp=ShowNextLast
response.write str_tmp
str_tmp=ShowPageInfo
response.write str_tmp
Response.write " "
ShowGoto
End Sub
'==============================================
'ShowFirstPrv 顯示首頁、前一頁
'
'
'==============================================
Private Function ShowFirstPrv()
Dim Str_tmp,int_prvpage
If int_curpage=1 Then
str_tmp=Btn_First&" "&Btn_Prev
Elseif int_curpage=0 then
str_tmp=Btn_First&" "&Btn_Prev
else
int_prvpage=int_curpage-1
str_tmp="<a href=""#"" onclick=""javascript:generalSubmit('1')"" alt=""第一頁"">" & Btn_First&"</a> <a href="" #"" onclick=""javascript:generalSubmit('"&int_prvpage&"')"" alt=""前一頁"">" & Btn_Prev&"</a>"
End If
ShowFirstPrv=str_tmp
End Function
'===============================================
'ShowNextLast 下一頁、末頁
'
'
'==============================================
Private Function ShowNextLast()
Dim str_tmp,int_Nextpage
If Int_curpage>=int_totalpage Then
str_tmp=Btn_Next & " " & Btn_Last
Else
Int_NextPage=int_curpage+1
str_tmp="<a href=""#"" onclick=""javascript:generalSubmit('"&int_nextpage&"')"" alt=""後一頁"">" & Btn_Next&"</a> <a href= ""#"" onclick=""javascript:generalSubmit('"&int_totalpage&"')"" alt=""最後一頁"">" & Btn_Last&"</a>"
End If
ShowNextLast=str_tmp
End Function
'End Function
'==============================================
'ShowNumBtn 修改後的數位導航
'
'==============================================
Function showNumBtn()
Dim i,str_tmp,end_page,start_page
start_page=1
'add by sll 2005.05.20 int_curpage=0
if int_curpage=0 then
str_tmp=str_tmp&"0"
else
if int_curpage>1 then
start_page=int_curpage
if (int_curpage<=5) then
start_page=1
end if
if (int_curpage>5) then
start_page=int_curpage-2
end if
end if
end_page=start_page+5
if end_page>int_totalpage then
end_page=int_totalpage
end if
For i=start_page to end_page
strTemp=XD_sURL & CStr(i)
str_tmp=str_tmp & "[<a href=""#"" onclick=""javascript:generalSubmit('"&i&"')"">"&i&"</a>] "
Next
end if
showNumBtn=str_tmp
End Function
'===============================================
'ShowGoto 頁面跳轉
'頁面自動跳轉
'add by sll 2005.05.20
'==============================================
Private Function ShowGoto()
'response.write int_totalPage
dim inti
if int_totalPage<=0 then
response.write "<select name='goto' disabled>"
Response.Write "<option value='0'>0</option>"
response.write "</select>"
else
response.write "<select name='goto' onchange='javascript:generalSubmit(this.value)'>"
for inti=1 to int_totalPage
Response.Write "<option value='"&inti&"'"
if cstr(inti)=cstr(int_curpage) then
response.write "selected"
end if
response.write" >"&inti&"</option>"
next
response.write "</select>"
end if
End Function
'==============================================
'ShowPageInfo 分頁信息
'根據要求自行修改
'
'==============================================
Private Function ShowPageInfo()
Dim str_tmp
str_tmp=" [頁次:<font color=red>"&int_curpage&"</font>/"&int_totalpage&"頁] [共"&int_totalrecord&"條] ["&XD_PageSize&"條/頁]"
ShowPageInfo=str_tmp
End Function
'==============================================
'ShowPageSizeChange 改變頁大小
'根據要求自行修改
'
'==============================================
public sub ShowPageSizeChange()
Dim str_tmp
str_tmp="頁大小:<select name='sssssPageSize' onchange='changePageSize(this.value)'>"
str_tmp=str_tmp & "<option"
if XD_PageSize=10 then str_tmp =str_tmp & " selected "
str_tmp=str_tmp & " value='10'>10</option>"
str_tmp=str_tmp & "<option"
if XD_PageSize=20 then str_tmp =str_tmp & " selected "
str_tmp=str_tmp & " value='20'>20</option>"
str_tmp=str_tmp & "<option"
if XD_PageSize=50 then str_tmp =str_tmp & " selected "
str_tmp=str_tmp & " value='50'>50</option>"
str_tmp=str_tmp & "<option"
if XD_PageSize=int_totalRecord then str_tmp =str_tmp & " selected "
str_tmp=str_tmp & " value='" & int_totalRecord & "'>all</option>"
str_tmp=str_tmp & "</select>"
response.Write str_tmp
描述 sub
'===============================================
'修改後的取得目前Url參數的函數
'Codeing by Redsun
'northsnow註:不知道用在何處,但是保留
'==============================================
Private Function GetUrl()
Dim ScriptAddress, M_ItemUrl, M_item
ScriptAddress = CStr(Request.ServerVariables("SCRIPT_NAME"))&"?" '取得目前位址
If (Request.QueryString <> "") Then
M_ItemUrl = ""
For Each M_item In Request.QueryString
If InStr("page",M_Item)=0 Then
M_ItemUrl = M_ItemUrl & M_Item &"="& Server.URLEncode(Request.QueryString(""&M_Item&"")) & "&"
End If
Next
ScriptAddress = ScriptAddress & M_ItemUrl '取得帶參數位址
End If
GetUrl = ScriptAddress '& "page="
End Function
'==============================================
' 設定Terminate 事件。
'==============================================
Private Sub Class_Terminate
'XD_RS.close
'Set XD_RS=nothing
End Sub
'==============================================
'ShowError 錯誤提示
'==============================================
Private Sub ShowError()
If str_Error <> "" Then
Response.Write("" & SW_Error & "")
Response.End
End If
End Sub
End class
% >