创建一个ASP通用分页类

从开始学习到使用ASP到现在也写了不少程序了,最令人头痛的是写数据分页,每次都是由于几个变量名或几个参数的不同,因而需要每次都写哪一段冗长而又繁杂的分页代码,代码长了使得程序的可读性变差,容易出差,调试半天也找不出错在哪里,所以慢慢的我开始使用一些网上的提供的分页函数或分页类。的确省事不少,但是通常的函数和类的做法都是就数据显示部分也封装了起来,每次为了达到自己需要的显求效果要去改动函数或者类的本身,所以使用起来也不是怎么方便,自己写的分页改起来已经够复杂了,更不要说别人的了。

         所以趁昨天有空自己写了一个分页的类,自我感觉良好(不要用鸡蛋砸我),在这里和大家分享一下自己的经验(谈不上经验,感想吧)。在这里我也不想说分页的原理了,反正大家都懂,要我往深入的谈我也不会。呵呵。

一、        创建分页类的目标

在写之前,我曾想过,我究竟要写怎么样一个类,回想起以前写分页过程的时候,最烦的莫过于每次都要写哪一段复杂的分页代码,最大的烦恼每次都是仅仅几个变量名的不同。所以第一个要实现的就是要把这个封装起来,第二个就是要把分页的导航条也封装起来,第三个,不习惯哪些把数据显示部分也封装起来的方法,这不是方便编程,对与哪些对显示效果每次都不同的用户来说,比自己写分页还要麻烦。所以我的目地就是对RecordSet进行一些简单的封装。

二、创建过程

所以我写的第一个属性,就是返一个经过处理的RecordSet

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

 


这个属性的作用是更据指定RecordSet 的当前面,并到指针指向当前页的第一条记录,这个应该就是整个类的完成分页的核心了,当然,其中的一些参数是靠其它的属性来获取,所以这里顺便介绍一个这个类所要的基本参数

'=============================================

'GetConn             得到数据库连接

'

'=============================================

Public Property Let GetConn(obj_Conn)

  Set XD_Conn=obj_Conn

End Property

 

'=============================================

'GetSQL                         得到查询语句

'

'==============================================

Public Property Let GetSQL(str_sql)

  XD_SQL=str_sql

End Property

'===============================================

'PageSize   属性

'设置每一页的分页大小

'===============================================

Public Property Let PageSize(int_PageSize)

  If IsNumeric(Int_Pagesize) Then

            XD_PageSize=CLng(int_PageSize)

  Else

            str_error=str_error & "PageSize参数不正确"

            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

以上几个是在使用类的过程必需要指定的参数,曾经我在写属性的时候对每个传入的参数加上IsObject(obj_conn)等判断,为的是类的健壮,但是后来想来想去,这个对与ASP来说没有必要,不加还能加快点速度,至于为什么这样,我想各位在使用过程中也会发现,加还不如不加。这也是我经过了思想斗争以后才去掉了,只保留了一些必要的验证。


还有一个参数就是当前页的获得,在程序中我用int_curpage来标识,这个的话放在类的创建过程中获得在好也没有了

Private Sub Class_Initialize

  '========================

  '设定一些参数的黙认值

  '========================

  XD_PageSize=10               '设定分页的默认值为10

  '========================

  '获取当前面的值

  '========================

  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

到这里这个类分的功能基本已经实现了,只要在调用这个类的页面的URL后面加上page=n,它就会显示第n页的内容了,所以接下去要做的就是创建一个数据导航条了,我把它设计为类似以面的形式

9 3[1] [2] [3] [4] [5] [6] [7] [8] 4 :页次:1/8 51条记录 7/每页

 

在页面里通过调用ShowPage()的方法显示出来,ShowPage可以在GetRS以后的任意位置调用,也可以调用多次

Public Sub ShowPage()

       Dim str_tmp

       int_totalRecord=XD_RS.RecordCount

       If int_totalRecord<=0 Then

              str_error=str_error & "总记录数为零,请输入数据"

              Call ShowError()

       End If

       If int_totalRecord<pagesize 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

      

       '=====================================================

       '显示分页信息,各个模块根据自己要求更改显求位置

       '=====================================================

       response.write "<table><tr><td>

       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 "</td></tr></table>"

      

End Sub

 

到这里类的功能才算完整(为了节省版面,我有些方法没有放上去,再下面附上全部完整代码)写一个简单页面测试一下

 

<!--#include file="conn.asp"-->

<!--#include file="xdownpage.inc"-->把分页类包含进来

<%

Set mypage=new xdownpage

mypage.getconn=conn

mypage.getsql="select * from productinfo order by id asc"

mypage.pagesize=7

set rs=mypage.getrs()得到Recordset

mypage.showpage()显示分页导航条

for i=1 to mypage.pagesize  下面的使用就和使用一般的recordset没有什么两样了

if not rs.eof then用来判断是否到达最后一条的,一定要加,不然最后一页分出问题

response.write rs("key") & "<br>"在这里就可以按自己的意思显示代码了,

rs.movenext

else

exit for

end if

 

next

效果还不错,该有的全有了。

 

分页过程中,还有一个比软麻烦的问题是,在带多个参数的URL中,如保证在页面转向的时候不掉失其它参数。我靠一个GetURL的过程来实现,并在生成导航时调用。

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)'得到当前页文件名

 

  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

通过GetURL的处理,可以自动的获取当前面的文件名,和所有带的参数,实现了页面转换页不丢失参数。

三、    后记

通过这个分页类,解决了每次分页时需要重复写的分页部分代码,方便了编程,也使的提高了主要代码的可读性。也希望能给大家在编程过程中带来一点方便,由于本人水平有限,程序和文章中难免有错,还望大家批评指正。




<%
'=====================================================================
'XDOWNPAGE   ASP版本
'版本   1.00
'Code by  zykj2000
'Email:   zykj_2000@163.net
'BBS:   http://bbs.513soft.net
'本程序可以免费使用、修改,希望我的程序能为您的工作带来方便
'但请保留以上请息
'
'程序特点
'本程序主要是对数据分页的部分进行了封装,而数据显示部份完全由用户自定义,
'支持URL多个参数
'
'使用说明
'程序参数说明
'PapgeSize      定义分页每一页的记录数
'GetRS       返回经过分页的Recordset此属性只读
'GetConn      得到数据库连接
'GetSQL       得到查询语句
'程序方法说明
'ShowPage      显示分页导航条,唯一的公用方法
'
'例:
' <!--#include file="conn.asp"-->
' <!--#include file="xdownpage.inc"-->  '包含文件
'
' Set mypage=new xdownpage   '创建对象
' mypage.getconn=conn    '得到数据库连接
' mypage.getsql="select * from productinfo order by id asc"
' mypage.pagesize=5    '设置每一页的记录条数据为5条
' set rs=mypage.getrs()    '返回Recordset
' mypage.showpage()    '显示分页信息,这个方法可以,在set rs=mypage.getrs()以后
'        任意位置调用,可以调用多次
' for i=1 to mypage.pagesize    '接下来的操作就和操作一个普通Recordset对象一样操作
'  if not rs.eof then   '这个标记是为了防止最后一页的溢出
'   response.write rs(0) & "<br>" '这里就可以自定义显示方式了
'   rs.movenext
'  else
'   exit for
'  end if
' next
'
'=====================================================================

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%"     '定义分页信息框大小

Class Xdownpage
Private XD_PageCount,XD_Conn,XD_Rs,XD_SQL,XD_PageSize,Str_errors,int_curpage,str_URL,int_totalPage,int_totalRecord


'=================================================================
'PageSize 属性
'设置每一页的分页大小
'=================================================================
Public Property Let PageSize(int_PageSize)
 If IsNumeric(Int_Pagesize) Then
  XD_PageSize=CLng(int_PageSize)
 Else
  str_error=str_error & "PageSize的参数不正确"
  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 属性
'返回分页后的记录集
'=================================================================
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  得到数据库连接
'
'================================================================ 
Public Property Let GetConn(obj_Conn)
 Set XD_Conn=obj_Conn
End Property

'================================================================
'GetSQL   得到查询语句
'
'================================================================
Public Property Let GetSQL(str_sql)
 XD_SQL=str_sql
End Property

'==================================================================
'Class_Initialize 类的初始化
'初始化当前页的值
'
'================================================================== 
Private Sub Class_Initialize
 '========================
 '设定一些参数的黙认值
 '========================
 XD_PageSize=10  '设定分页的默认值为10
 '========================
 '获取当前面的值
 '========================
 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  创建分页导航条
'有首页、前一页、下一页、末页、还有数字导航
'
'====================================================================
Public Sub ShowPage()
 Dim str_tmp
 
 int_totalRecord=XD_RS.RecordCount
 If int_totalRecord<=0 Then
  str_error=str_error & "总记录数为零,请输入数据"
  Call ShowError()
 End If
 If int_totalRecord<pagesize 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
 
 '===============================================================================
 '显示分页信息,各个模块根据自己要求更改显求位置
 '===============================================================================
 response.write "<table border=0  width="&XD_Width&"><tr><td align="&XD_Align&">"
 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 "</td></tr></table>"
 
End Sub

'====================================================================
'ShowFirstPrv  显示首页、前一页
'
'
'====================================================================
Private Function ShowFirstPrv()
 Dim Str_tmp,int_prvpage
 If int_curpage=1 Then
  str_tmp=Btn_First&"&nbsp;"&Btn_Prev
 Else
  int_prvpage=int_curpage-1
  str_tmp="<a href="&geturl&"1>"&Btn_First&"</a>&nbsp;<a href="&geturl & int_prvpage &">"& 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 & "&nbsp;" & Btn_Last
 Else
  Int_NextPage=int_curpage+1
  str_tmp="<a href="& geturl & int_NextPage &">"&Btn_Next&"</a>&nbsp;<a href="&geturl & int_totalpage &">"& Btn_Last&"</a>"
 End If
 ShowNextLast=str_tmp
End Function


'====================================================================
'ShowNumBtn  数字导航
'
'
'====================================================================
Private Function showNumBtn()
 Dim i,str_tmp
 For i=1 to int_totalpage
  str_tmp=str_tmp & "<a href="& geturl & i &">["&i&"]</a>&nbsp;"
 Next
 showNumBtn=str_tmp
 
End Function


'====================================================================
'ShowPageInfo  分页信息
'更据要求自行修改
'
'====================================================================
Private Function ShowPageInfo()
 Dim str_tmp
 str_tmp="页次:"&int_curpage&"/"&int_totalpage&"页 共"&int_totalrecord&"条记录 "&XD_PageSize&"条/每页"
 ShowPageInfo=str_tmp
End Function
'==================================================================
'GetURL  得到当前的URL
'更据URL参数不同,获取不同的结果
'
'==================================================================
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)'得到当前页文件名
 
 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

'====================================================================
' 设置 Terminate 事件。
'
'====================================================================
Private Sub Class_Terminate  
 XD_RS.close
 Set XD_RS=nothing
End Sub
'====================================================================
'ShowError  错误提示
'
'
'====================================================================
Private Sub ShowError()
 If str_Error <> "" Then
  Response.Write("<font color=""#FF0000""><br><b>" & SW_Error & "</font>")
  Response.End
 End If
End Sub

End class



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=16829

原文地址:https://www.cnblogs.com/MaxIE/p/350205.html