创建一个ASP通用分页类 |
|
本文标签:创建一个ASP通用分页类 从开始学习到使用ASP到现在也写了不少程序了,最令人头痛的是写数据分页,每次都是由于几个变量名或几个参数的不同,因而需要每次都写哪一段冗长而又繁杂的分页代码,代码长了使得程序的可读性变差,容易出差,调试半天也找不出错在哪里,所以慢慢的我开始使用一些网上的提供的分页函数或分页类 。的确省事不少,但是通常的函数和类的做法都是就数据显示部分也封装了起来,每次为了达到自己需要的显求效果要去改动函数或者类的本身,所以使用起来也不是怎么方便,自己写的分页改起来已经够复杂了,更不要说别人的了 。
所以趁昨天有空自己写了一个分页的类,自我感觉良好(不要用鸡蛋砸我),在这里和大家分享一下自己的经验(谈不上经验,感想吧) 。在这里我也不想说分页的原理了,反正大家都懂,要我往深入的谈我也不会 。呵呵 。 一、创建分页类的目标 二、创建过程 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![]() 一个参数就是当前页的获得,在程序中我用int_curpage来标识,这个的话放在类的创建过程中获得在好也没有了 ======================== 设定一些参数的黙认值 ======================== 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 Sub9 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="" 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 " 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![]() 到这里类的功能才算完整(为了节省版面,我有些方法没有放上去,再下面附上全部完整代码)写一个简单页面测试一下 <% 把分页类包含进来 set conn = server.CreateObject("adodb.connection") conn.open "driver={microsoft access driver (*.mdb)};dbq=" & server.Mappath("pages.mdb")![]() ![]() #############类调用样例################# 创建对象 Set mypage=new xdownpage 得到数据库连接 mypage.getconn=conn sql语句 mypage.getsql="select * from [test] order by id asc" 设置每一页的记录条数据为5条 mypage.pagesize=5 返回Recordset set rs=mypage.getrs() 显示分页信息,这个方法可以,在set rs=mypage.getrs()以后,可在任意位置调用,可以调用多次 mypage.showpage()![]() 显示数据 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 %>![]() 效果还不错,该有的全有了 。 分页过程中,还有一个比软麻烦的问题是,在带多个参数的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的处理,可以自动的获取当前面的文件名,和所有带的参数,实现了页面转换页不丢失参数 。 全部代码下载 附全部代码: |