創(chuàng)建一個(gè)ASP通用分頁(yè)類
從開(kāi)始學(xué)習(xí)到使用asp到現(xiàn)在也寫(xiě)了不少程序了,最令人頭痛的是寫(xiě)數(shù)據(jù)分頁(yè),每次都是由于幾個(gè)變量名或幾個(gè)參數(shù)的不同,因而需要每次都寫(xiě)哪一段冗長(zhǎng)而又繁雜的分頁(yè)代碼,代碼長(zhǎng)了使得程序的可讀性變差,容易出差,調(diào)試半天也找不出錯(cuò)在哪里,所以慢慢的我開(kāi)始使用一些網(wǎng)上的提供的分頁(yè)函數(shù)或分頁(yè)類。的確省事不少,但是通常的函數(shù)和類的做法都是就數(shù)據(jù)顯示部分也封裝了起來(lái),每次為了達(dá)到自己需要的顯求效果要去改動(dòng)函數(shù)或者類的本身,所以使用起來(lái)也不是怎么方便,自己寫(xiě)的分頁(yè)改起來(lái)已經(jīng)夠復(fù)雜了,更不要說(shuō)別人的了。

所以趁昨天有空自己寫(xiě)了一個(gè)分頁(yè)的類,自我感覺(jué)良好(不要用雞蛋砸我),在這里和大家分享一下自己的經(jīng)驗(yàn)(談不上經(jīng)驗(yàn),感想吧)。在這里我也不想說(shuō)分頁(yè)的原理了,反正大家都懂,要我往深入的談我也不會(huì)。呵呵。

一、創(chuàng)建分頁(yè)類的目標(biāo)
在寫(xiě)之前,我曾想過(guò),我究竟要寫(xiě)怎么樣一個(gè)類,回想起以前寫(xiě)分頁(yè)過(guò)程的時(shí)候,最煩的莫過(guò)于每次都要寫(xiě)哪一段復(fù)雜的分頁(yè)代碼,最大的煩惱每次都是僅僅幾個(gè)變量名的不同。所以第一個(gè)要實(shí)現(xiàn)的就是要把這個(gè)封裝起來(lái),第二個(gè)就是要把分頁(yè)的導(dǎo)航條也封裝起來(lái),第三個(gè),不習(xí)慣哪些把數(shù)據(jù)顯示部分也封裝起來(lái)的方法,這不是方便編程,對(duì)與哪些對(duì)顯示效果每次都不同的用戶來(lái)說(shuō),比自己寫(xiě)分頁(yè)還要麻煩。所以我的目地就是對(duì)recordset進(jìn)行一些簡(jiǎn)單的封裝。

二、創(chuàng)建過(guò)程
所以我寫(xiě)的第一個(gè)屬性,就是返一個(gè)經(jīng)過(guò)處理的recordse

public property get getrs()
  
set xd_rs=server.createobject("adodb.recordset")
  xd_rs.pagesize
=pagesize
  xd_rs.open xd_sql,xd_conn,
1,1
  
if not(xd_rs.eof and xd_rs.bof) then
  
if int_curpage>xd_rs.pagecount then
int_curpage
=xd_rs.pagecount
  
end if
  xd_rs.absolutepage
=int_curpage
  
end if
  
set getrs=xd_rs
end property

這個(gè)屬性的作用是更據(jù)指定recordset 的當(dāng)前面,并到指針指向當(dāng)前頁(yè)的第一條記錄,這個(gè)應(yīng)該就是整個(gè)類的完成分頁(yè)的核心了,當(dāng)然,其中的一些參數(shù)是靠其它的屬性來(lái)獲取,所以這里順便介紹一個(gè)這個(gè)類所要的基本參數(shù)
=============================================
'getconn 得到數(shù)據(jù)庫(kù)連接
'
'
=============================================
public property let getconn(obj_conn)
  
set xd_conn=obj_conn
end property

'=============================================
'
getsql 得到查詢語(yǔ)句
'
'
==============================================
public property let getsql(str_sql)
  xd_sql
=str_sql
end property

'===============================================
'
pagesize 屬性
'
設(shè)置每一頁(yè)的分頁(yè)大小
'
===============================================
public property let pagesize(int_pagesize)
  
if isnumeric(int_pagesize) then
  xd_pagesize
=clng(int_pagesize)
  
else
  str_error
=str_error & "pagesize的參數(shù)不正確"
  showerror()
  
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
以上幾個(gè)是在使用類的過(guò)程必需要指定的參數(shù),曾經(jīng)我在寫(xiě)屬性的時(shí)候?qū)γ總€(gè)傳入的參數(shù)加上isobject(obj_conn)等判斷,為的是類的健壯,但是后來(lái)想來(lái)想去,這個(gè)對(duì)與asp來(lái)說(shuō)沒(méi)有必要,不加還能加快點(diǎn)速度,至于為什么這樣,我想各位在使用過(guò)程中也會(huì)發(fā)現(xiàn),加還不如不加。這也是我經(jīng)過(guò)了思想斗爭(zhēng)以后才去掉了,只保留了一些必要的驗(yàn)證。
一個(gè)參數(shù)就是當(dāng)前頁(yè)的獲得,在程序中我用int_curpage來(lái)標(biāo)識(shí),這個(gè)的話放在類的創(chuàng)建過(guò)程中獲得在好也沒(méi)有了
 '========================
  '設(shè)定一些參數(shù)的黙認(rèn)值
  '========================
  xd_pagesize=10 '設(shè)定分頁(yè)的默認(rèn)值為10
  '========================
  '獲取當(dāng)前面的值
  '========================
  if request("page")="" then
  int_curpage
=1
  
elseif not(isnumeric(request("page"))) then
  int_curpage
=1
  
elseif cint(trim(request("page")))<1 then
  int_curpage
=1
  
else
  int_curpage
=cint(trim(request("page")))
  
end if  
end sub
到這里這個(gè)類分的功能基本已經(jīng)實(shí)現(xiàn)了,只要在調(diào)用這個(gè)類的頁(yè)面的url后面加上page=n,它就會(huì)顯示第n頁(yè)的內(nèi)容了,所以接下去要做的就是創(chuàng)建一個(gè)數(shù)據(jù)導(dǎo)航條了,我把它設(shè)計(jì)為類似以面的形式

9 3[1] [2] [3] [4] [5] [6] [7] [8] 4 :頁(yè)次:1/8頁(yè) 共51條記錄 7條/每頁(yè)

在頁(yè)面里通過(guò)調(diào)用showpage()的方法顯示出來(lái),showpage可以在getrs以后的任意位置調(diào)用,也可以調(diào)用多次
public sub showpage()
dim str_tmp
int_totalrecord
=xd_rs.recordcount
if int_totalrecord<=0 then
  str_error
=str_error & "總記錄數(shù)為零,請(qǐng)輸入數(shù)據(jù)"
  
call showerror()
end if
if int_totalrecord="" then
  int_totalpage
=1
else
  
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

'=====================================================
'
顯示分頁(yè)信息,各個(gè)模塊根據(jù)自己要求更改顯求位置
'
=====================================================
response.write "
str_tmp=showfirstprv '顯示首頁(yè)、前一頁(yè)
response.write str_tmp  
str_tmp
=shownumbtn '數(shù)字導(dǎo)航
response.write str_tmp
str_tmp
=shownextlast  '下一頁(yè)、末頁(yè)
response.write str_tmp
str_tmp
=showpageinfo
response.write str_tmp
response.write 
""
end sub

到這里類的功能才算完整(為了節(jié)省版面,我有些方法沒(méi)有放上去,再下面附上全部完整代碼)寫(xiě)一個(gè)簡(jiǎn)單頁(yè)面測(cè)試一下

<
'把分頁(yè)類包含進(jìn)來(lái)
set conn = server.createobject("adodb.connection")
conn.open 
"driver={microsoft access driver (*.mdb)};dbq=" & server.mappath("pages.mdb")


'#############類調(diào)用樣例#################
'
創(chuàng)建對(duì)象
set mypage=new xdownpage
'得到數(shù)據(jù)庫(kù)連接
mypage.getconn=conn
'sql語(yǔ)句
mypage.getsql="select * from [test] order by id asc"
'設(shè)置每一頁(yè)的記錄條數(shù)據(jù)為5條
mypage.pagesize=5
'返回recordset
set rs=mypage.getrs()
'顯示分頁(yè)信息,這個(gè)方法可以,在set rs=mypage.getrs()以后,可在任意位置調(diào)用,可以調(diào)用多次
mypage.showpage()

'顯示數(shù)據(jù)
response.write("
")
for i=1 to mypage.pagesize
'這里就可以自定義顯示方式了
    if not rs.eof then 
        response.write rs(
0& "
"
        rs.movenext
    
else
         
exit for
    
end if
next
%
>

效果還不錯(cuò),該有的全有了。

分頁(yè)過(guò)程中,還有一個(gè)比軟麻煩的問(wèn)題是,在帶多個(gè)參數(shù)的url中,如保證在頁(yè)面轉(zhuǎn)向的時(shí)候不掉失其它參數(shù)。我靠一個(gè)geturl的過(guò)程來(lái)實(shí)現(xiàn),并在生成導(dǎo)航時(shí)調(diào)用。

private function geturl()
  
dim strurl,str_url,i,j,search_str,result_url
  search_str
="page="
  strurl
=request.servervariables("url")
  strurl
=split(strurl,"/")
  i
=ubound(strurl,1)
  str_url
=strurl(i)'得到當(dāng)前頁(yè)文件名
  str_params=request.servervariables("query_string")
  
if str_params="" then
  result_url
=str_url & "?page="
  
else
  
if instrrev(str_params,search_str)=0 then
result_url
=str_url & "?" & str_params &"&page="
  
else
j
=instrrev(str_params,search_str)-2
if j=-1 then
  result_url
=str_url & "?page="
else
  str_params
=left(str_params,j)
  result_url
=str_url & "?" & str_params &"&page="
end if
  
end if
  
end if
  geturl
=result_url
end function

通過(guò)geturl的處理,可以自動(dòng)的獲取當(dāng)前面的文件名,和所有帶的參數(shù),實(shí)現(xiàn)了頁(yè)面轉(zhuǎn)換頁(yè)不丟失參數(shù)。
三、后記
通過(guò)這個(gè)分頁(yè)類,解決了每次分頁(yè)時(shí)需要重復(fù)寫(xiě)的分頁(yè)部分代碼,方便了編程,也使的提高了主要代碼的可讀性。也希望能給大家在編程過(guò)程中帶來(lái)一點(diǎn)方便,由于本人水平有限,程序和文章中難免有錯(cuò),還望大家批評(píng)指正。

附全部代碼:
<% '=================================================================== 'xdownpage asp版本 '版本 1.00 'code by zykj2000 'email: zykj_2000@163.net 'bbs: http://bbs.513soft.net '本程序可以免費(fèi)使用、修改,希望我的程序能為您的工作帶來(lái)方便 '但請(qǐng)保留以上請(qǐng)息 ' '程序特點(diǎn) '本程序主要是對(duì)數(shù)據(jù)分頁(yè)的部分進(jìn)行了封裝,而數(shù)據(jù)顯示部份完全由用戶自定義, '支持url多個(gè)參數(shù) ' '使用說(shuō)明 '程序參數(shù)說(shuō)明 'papgesize 定義分頁(yè)每一頁(yè)的記錄數(shù) 'getrs 返回經(jīng)過(guò)分頁(yè)的recordset此屬性只讀 'getconn 得到數(shù)據(jù)庫(kù)連接 'getsql 得到查詢語(yǔ)句 '程序方法說(shuō)明 'showpage 顯示分頁(yè)導(dǎo)航條,唯一的公用方法 ' '=================================================================== const btn_first="<font face=""webdings"">9</font>" '定義第一頁(yè)按鈕顯示樣式 const btn_prev="<font face=""webdings"">3</font>" '定義前一頁(yè)按鈕顯示樣式 const btn_next="<font face=""webdings"">4</font>" '定義下一頁(yè)按鈕顯示樣式 const btn_last="<font face=""webdings"">:</font>" '定義最后一頁(yè)按鈕顯示樣式 const xd_align="center" '定義分頁(yè)信息對(duì)齊方式 const xd_width="100%" '定義分頁(yè)信息框大小 class xdownpage private xd_pagecount,xd_conn,xd_rs,xd_sql,xd_pagesize,str_errors,int_curpage,str_url,int_totalpage,int_totalrecord,xd_surl '================================================================= 'pagesize 屬性 '設(shè)置每一頁(yè)的分頁(yè)大小 '================================================================= public property let pagesize(int_pagesize) if isnumeric(int_pagesize) then xd_pagesize=clng(int_pagesize) else str_error=str_error & "pagesize的參數(shù)不正確" showerror() 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 屬性 '返回分頁(yè)后的記錄集 '================================================================= public property get getrs() set xd_rs=server.createobject("adodb.recordset") xd_rs.pagesize=pagesize xd_rs.open xd_sql,xd_conn,1,1 if not(xd_rs.eof and xd_rs.bof) then if int_curpage>xd_rs.pagecount then int_curpage=xd_rs.pagecount end if xd_rs.absolutepage=int_curpage end if set getrs=xd_rs end property '================================================================ 'getconn 得到數(shù)據(jù)庫(kù)連接 ' '================================================================ public property let getconn(obj_conn) set xd_conn=obj_conn end property '================================================================ 'getsql 得到查詢語(yǔ)句 ' '================================================================ public property let getsql(str_sql) xd_sql=str_sql end property '================================================================== 'class_initialize 類的初始化 '初始化當(dāng)前頁(yè)的值 ' '================================================================== private sub class_initialize '======================== '設(shè)定一些參數(shù)的黙認(rèn)值 '======================== xd_pagesize=10 '設(shè)定分頁(yè)的默認(rèn)值為10 '======================== '獲取當(dāng)前面的值 '======================== if request("page")="" then int_curpage=1 elseif not(isnumeric(request("page"))) then int_curpage=1 elseif cint(trim(request("page")))<1 then int_curpage=1 else int_curpage=cint(trim(request("page"))) end if end sub '==================================================================== 'showpage 創(chuàng)建分頁(yè)導(dǎo)航條 '有首頁(yè)、前一頁(yè)、下一頁(yè)、末頁(yè)、還有數(shù)字導(dǎo)航 ' '==================================================================== public sub showpage() dim str_tmp xd_surl = geturl() int_totalrecord=xd_rs.recordcount if int_totalrecord<=0 then str_error=str_error & "總記錄數(shù)為零,請(qǐng)輸入數(shù)據(jù)" call showerror() end if if int_totalrecord="" then int_totalpage=1 else 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 '================================================================== '顯示分頁(yè)信息,各個(gè)模塊根據(jù)自己要求更改顯求位置 '================================================================== 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 "" end sub '==================================================================== 'showfirstprv 顯示首頁(yè)、前一頁(yè) ' ' '==================================================================== private function showfirstprv() dim str_tmp,int_prvpage if int_curpage=1 then str_tmp=btn_first&" "&btn_prev else int_prvpage=int_curpage-1 str_tmp="<a href="""&xd_surl & "1" & """>" & btn_first&"</a> <a href=""" & xd_surl & cstr(int_prvpage) & """>" & btn_prev&"</a>" end if showfirstprv=str_tmp end function '==================================================================== 'shownextlast 下一頁(yè)、末頁(yè) ' ' '==================================================================== 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=""" & xd_surl & cstr(int_nextpage) & """>" & btn_next&"</a> <a href="""& xd_surl & cstr(int_totalpage) & """>" & btn_last&"</a>" end if shownextlast=str_tmp end function '==================================================================== 'shownumbtn 數(shù)字導(dǎo)航 ' ' '==================================================================== private function shownumbtn() dim i,str_tmp for i=1 to int_totalpage str_tmp=str_tmp & "[<a href=""" & xd_surl & cstr(i) & """>"&i&"</a>] " next shownumbtn=str_tmp end function '==================================================================== 'showpageinfo 分頁(yè)信息 '更據(jù)要求自行修改 ' '==================================================================== private function showpageinfo() dim str_tmp str_tmp="頁(yè)次:"&int_curpage&"/"&int_totalpage&"頁(yè) 共"&int_totalrecord&"條記錄 "&xd_pagesize&"條/每頁(yè)" showpageinfo=str_tmp end function '================================================================== 'geturl 得到當(dāng)前的url '更據(jù)url參數(shù)不同,獲取不同的結(jié)果 ' '================================================================== private function geturl() dim strurl,str_url,i,j,search_str,result_url search_str="page=" strurl=request.servervariables("url") strurl=split(strurl,"/") i=ubound(strurl,1) str_url=strurl(i)'得到當(dāng)前頁(yè)文件名 str_params=trim(request.servervariables("query_string")) if str_params="" then result_url=str_url & "?page=" else if instrrev(str_params,search_str)=0 then result_url=str_url & "?" & str_params &"&page=" else j=instrrev(str_params,search_str)-2 if j=-1 then result_url=str_url & "?page=" else str_params=left(str_params,j) result_url=str_url & "?" & str_params &"&page=" end if end if end if geturl=result_url end function '==================================================================== ' 設(shè)置 terminate 事件。 ' '==================================================================== private sub class_terminate xd_rs.close set xd_rs=nothing end sub '==================================================================== 'showerror 錯(cuò)誤提示 ' ' '==================================================================== private sub showerror() if str_error <> "" then response.write("" & str_error & "") response.end end if end sub end class set conn = server.createobject("adodb.connection") conn.open "driver={microsoft access driver (*.mdb)};dbq=" & server.mappath("pages.mdb") '#############類調(diào)用樣例################# '創(chuàng)建對(duì)象 set mypage=new xdownpage '得到數(shù)據(jù)庫(kù)連接 mypage.getconn=conn 'sql語(yǔ)句 mypage.getsql="select * from [test] order by id asc" '設(shè)置每一頁(yè)的記錄條數(shù)據(jù)為5條 mypage.pagesize=5 '返回recordset set rs=mypage.getrs() '顯示分頁(yè)信息,這個(gè)方法可以,在set rs=mypage.getrs()以后,可在任意位置調(diào)用,可以調(diào)用多次 mypage.showpage() '顯示數(shù)據(jù) response.write("<br/>") for i=1 to mypage.pagesize '這里就可以自定義顯示方式了 if not rs.eof then response.write rs(0) & "<br/>" rs.movenext else exit for end if next %>

相關(guān)文章
亚洲国产精品第一区二区,久久免费视频77,99V久久综合狠狠综合久久,国产免费久久九九免费视频