自己做的一个十分好用的asp分页类
做asp分页时,最麻烦的是拼分页导航条。一般的分页导航条是这样的: 共xx条 xx页 xx/每页 首页 上一页 下一页 末页 跳xx页。如果每次都在asp里拼写这些代码,显然太麻烦了。这里我做了一个asp类,解决了这个问题。先看类文件:mypages.inc
<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>
'****************************************
'asp分页类
'作者:蓝色代码 原创 转载请注明
'日期:2007-11-22
'相关网址:[url][/url]
'****************************************
Class mypages
private m_rs
dim m_thepage
dim m_url
dim m_pagesize
dim m_kk,lurl,m_i
private all_url
public Sub Class_Initialize
m_pagesize=15 '每页记录数,默认15条
pagecount=1 '总页数
end sub
public property let rs(krs)
m_rs=krs
end property
public property let url(vdate)
m_url=vdate
end property
public property let pagesize(krs)
m_pagesize=krs
end property
'当前页
public property let thepage(krs)
if krs="" then
m_thepage="1"
else
m_thepage=krs
end if
end property
Public Property Get theindex()
theindex=m_kk
End Property
Public Property Get rs()
rs=m_rs
End Property
Public Property Get pagesize()
pagesize=m_pagesize
End Property
'当前页第一条记录的序号
Public Property Get i()
i=m_i
End Property
'执行分页
public sub dopages(vrs)
set m_rs=server.createobject("adodb.recordset")
set m_rs=vrs
if vrs.eof and vrs.bof then
//donothing
m_i=0
else
m_rs.pagesize=m_pagesize
m_rs.absolutepage=cint(m_thepage)
m_i=(cint(m_thepage)-1)*m_pagesize+1
end if
end sub
'添加参数,a-参数名 b-参数值
public Sub add(a,b)
all_url=all_url & "&" & a & "=" & b
end sub
'得到导航条
public function getpage()
if m_rs.eof and m_rs.bof then
getpage=""
end if
dim theurl
theurl=m_url
dim lurl
lurl=all_url
dim ipage
ipage=cint(m_thepage)
if ipage<m_rs.pagecount then
npage="<a href='" & m_url & "?thepage=" & trim(cstr(ipage+1)) & "" & lurl & "'>下一页</a>"
else
npage="下一页"
end if
if ipage>1 then
fpage="<a href='" & m_url & "?thepage=" & trim(cstr(ipage-1)) & "" & lurl & "'>上一页</a>"
else
fpage="上一页"
end if
apage="<a href='" & m_url & "?thepage=1" & "" & lurl & "'>首页</a>"
zpage="<a href='" & m_url & "?thepage=" & trim(cstr(m_rs.pagecount)) & "" & lurl & "'>尾页</a>"
dim re
re=re & "<table width=99% border=0 align=center cellpadding=2 cellspacing=1>" & vbCrLf
re=re & "<tr>" & chr(13)
re=re & "<td align=right>共:**total**条 **ye**页 第**thepage/pagecount**页 **apage** **fpage** **npage** **zpage** <input name=""thepage"" type=""text"" id=""thepage"" value=""1"" size=""3"">" & vbCrLf
re=replace(re,"**total**",cstr(m_rs.recordcount))
re=replace(re,"**thepage/pagecount**",m_thepage & "/" & cstr(m_rs.pagecount))
re=replace(re,"**apage**",apage)
re=replace(re,"**zpage**",zpage)
re=replace(re,"**fpage**",fpage)
re=replace(re,"**npage**",npage)
re=replace(re,"**ye**",cstr(m_rs.pagecount))
re=re & "<a href=""javascript:gourl();"">GO</a></td>" & vbCrLf
re=re & "</tr>" & vbCrLf
re=re & "</table>" & vbCrLf
re=re & chr(13) & "<" & "script language=JavaScript>"
re=re & chr(13) & "function gourl(){"
re=re & chr(13) & "document.location=""" & m_url & "?thepage=""+document.all.thepage.value+""" & lurl & """;"
re=re & chr(13) & "}"
re=re & chr(13) & "<" & "/script>"
getpage=re
end function
Private Sub Class_Terminate
m_rs.close
set m_rs=nothing
end sub
end class
</script>
[color=#000000]分页类的使用方法:[/color]
<%
'创建记录集
set rs=createobject("adodb.recordset")
'sql语句
sql="select * from table where 1=1 "
'拼where语句,可拼多个
if request("a")<>"" then
sql=sql & " and 字段a='" & a & "' "
end if
if request("b")<>"" then
sql=sql & " and 字段b='" & b & "' "
end if
'打开记录集
rs.open sql,conn,1,1
'创建分页对象
set mypage=new mypages
mypage.pagesize=20 '每页20条记录
'当前页,注意是字符型的,如果这个参数为空,分页类会自动置为'1'
mypage.thepage=request("thepage")
'添加参数,一般分页导航条会生成xx.asp?thepage=2&a=xx&b=xx这样的url,add方法就是把这些参数加进来
mypage.add "a",request("a")
mypage.add "b",request("b")
'执行分页操作
mypage.dopages(rs)
'得到导航条
pageshow=mypage.getpage
'以下显示
i=mypage.i '本页面第一个序号
do while not rs.eof
[color=#000066]%>[/color]
序号:<%=i%> 姓名:<%=rs("a")%> <br>
<%
rs.movenext
i=i+1
if i>mypage.pagesize+mypage.i then exit do '记录数够了后退出
loop
rs.close
%>
<!--显示共:xx条 xx页 首页 第一页 下一页 末页 跳x页-->
页面导航:<%=pageshow%>