当前位置: 首页 > 图文教程 > 网络编程 > ASP > 创建一个ASP通用分页类(二)

ASP
asp分页(自己整理的2个分页程序)
ASP常用的系统配置函数
雨哲防采集策略之列表篇
雨哲浅谈关于防采集而不影响收录内容篇
asp数据库连接函数
ajax XMLHTTP Post Form时的表单乱码综合解决
ASP注入详细命令40条
用ASP实现MSSQL用户密码破解
ASP创建对象的两种方法比较
ASP字符串转换为整形、双精度型、布尔
ASP计算str2在str1中出现的次数
asp采集抓取网上房产信息的代码
ASP注册登陆实例代码
ASP中使用FileSystemObject时提高性能的方法
分享一个好东东,动态Include文件 (Dynamic File Includes)
解决 JScript 中使用日期类型数据时出现类型错误的问题
在ASP里面创建GUID
在JScript中使用缓存技术的实际代码
ASP javascript Application对象的Contents和StaticObjects做Cache的一些经验
海阳2006+功能中的潜水王

创建一个ASP通用分页类(二)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 99 ::
收藏到网摘: n/a

我们继续接上期

在页面里通过调用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的处理,可以自动的获取当前面的文件名,和所有带的参数,实现了页面转换页不丢失参数。
三、后记
通过这个分页类,解决了每次分页时需要重复写的分页部分代码,方便了编程,也使的提高了主要代码的可读性。也希望能给大家在编程过程中带来一点方便,由于本人水平有限,程序和文章中难免有错,还望大家批评指正。

全部代码下载