先将分页类源代码发上来,随后有空会发一些实例来讲解其用法,以及如何扩展其功能。
类原代码如下:
< %
'==============================================
'XDOWNPAGE ASP版本
'当前版本:2.0
'
'
'原版本 1.00
'Code by zykj2000
'Email:
'BBS:
'
'
'升级版本:1.5 (asp + oracle)
updated by doublel
Email:
blog: ttp://blog.csdn.net/doublel/
'升级说明:
'
'
'升级版本:2.0 ' (asp + oracle) ---->当前版本
'保留原名:XDOWNPAGE
Updated by northsnow
'email:
'blog:
'升级说明:
'1 , 数据查询时只查询当前页所包含的记录,大大降低了数据传输量
'2 , 如果正常的页导航,不用每次都查询总记录数,只要第一次查询后,后来通过参数传递即可
'3 , 支持动态更改页大小
'4 , 支持动态排序
'5 , 本程序只支持oracle,如果想用在sqlserver或者其他类型的数据库,请自行修改:Public Property Let GetSQL(str_sql)即可。
'
'
'其他程序修改者信息,请在源代码中查对!!!
'本程序可以免费使用、修改、复制、转载、引用,希望我的程序能为您的工作带来方便
'但请保留以上请息,特别是是原著信息。另外如果作为商业用途,请与原著和该版本升级
'人联系以取得许可。
'
'
'程序特点
'本程序主要是对数据分页的部分进行了封装,而数据显示部份完全由用户自定义,
'支持URL多个参数
'
'使用说明
'程序参数说明
'PapgeSize 定义分页每一页的记录数
'GetRS 返回经过分页的Recordset此属性只读
'GetConn 得到数据库连接
'GetSQL 得到查询语句
'totalRecordCount 传递总记录数
'程序方法说明
'ShowPage 显示分页导航条,唯一的公用方法
'ShowPageSizeChange() 显示改变页大小的列表
'
'例:
'
' '包含文件
'
' Set mypage=new xdownpage '创建对象
' mypage.getconn=conn '得到数据库连接
' mypage.getsql="select * from productinfo order by id asc"
' mypage.pagesize=5 '设置每一页的记录条数据为5条
'mypage.totalRecordCount=rsTotalCount 设置总记录数
' set rs=mypage.getrs() '返回Recordset
'mypage.GetSubmitForm="frmQuery" ' 分页默认提交的表单,currentpage参数
'Response.write(mypage.GetSubmitForm1()) '输出分页提交的函数
' mypage.showpage() '显示分页信息,这个方法可以,在set rs=mypage.getrs()以后
' 任意位置调用,可以调用多次
' do while not rs.eof '接下来的操作就和操作一个普通Recordset对象一样操作
' response.write rs(0) & "
' " '这里就可以自定义显示方式了
' rs.movenext
' loop
'
'添加了保存当前页面数量的提交脚本
'函数为GetSubmitForm()
'需要提交给函数GetSubmitForm一个表单名字
'在这个提交的表单里面保存变量flag,currentpage,pagesize,rsTotalCount 四个参数
'例子如下
'flag=request("flag")
'currentpage=request("currentpage")
'currentpage=request("pagesize")
'currentpage=request("rsTotalCount")
'在提交的表单里面加入下面四个input
'<input name="flag" type="hidden" value="< % =flag% >">
'<input name="currentpage" type="hidden" value="< % =currentpage% >">
'<input name="pagesize" type="hidden" value="< % =pagesize% >">
'<input name="rsTotalCount" type="hidden" value="< % =rsTotalCount% >">
'==============================================
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%" '定义分页信息框大小
Const XD_Height="20"
Class Xdownpage '类 从这里开始
'变量定义
public int_totalPage '总页数
public int_curcount '当前页的记录数
public XD_PageSize '页大小
Private int_curpage '当前页号
Private int_totalRecord '总记录数
Private XD_Conn '数据库连接对象
Private XD_Rs '记录集对象
Private XD_SQL '主sql语句
Private XD_Count_SQL '查询总记录数的sql语句
Private Str_errors
Private str_URL
Private XD_sURL
Private SubmitForm '所需的查询表单名字(隐藏表单名字)
'=================================================================
'PageSize 属性
'设置每一页的分页大小
'=================================================================
Public Property Let PageSize(int_PageSize)
If IsNumeric(Int_Pagesize) Then
if clng(Int_Pagesize)>0 then
XD_PageSize=CLng(int_PageSize)
else
XD_PageSize=10
end if
Else
XD_PageSize=10
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.CursorLocation=3
XD_Rs.Open XD_SQL,XD_Conn,3,1
int_curcount=XD_Rs.RecordCount
if int_totalRecord="" or not isNumeric(int_totalRecord) then int_totalRecord=0 '规范化int_totalRecord的值
if int_totalRecord=0 and (int_curcount>=PageSize or int_curpage>1) then call queryRsCount() '查询总记录数
if err.number<>0 then
Response.Write err.Clear
end if
Set GetRs=XD_RS
End Property
'=================================================================
'queryRSCount 方法
'查询总记录数
'=================================================================
Public sub queryRsCount()
'下面代码用于计算总记录数
if XD_Count_SQL<>"" then
set rs_sqlcount=server.createobject("adodb.recordset")
rs_sqlcount.CursorLocation=3
rs_sqlcount.open XD_Count_SQL,conn,3,1
if (rs_sqlcount.eof and rs_sqlcount.bof) then
int_totalRecord=0
else
int_totalRecord=rs_sqlcount(0)
int_totalRecord=clng(int_totalRecord)
end if
rs_sqlcount.close
set rs_sqlcount=nothing
end if
End sub
'================================================================
'GetConn 得到数据库连接
'
'================================================================
Public Property Let GetConn(obj_Conn)
Set XD_Conn=obj_Conn
End Property
'================================================================
'GetSQL 得到查询语句
'
'================================================================
Public Property Let GetSQL(str_sql)
if (str_sql<>"") then
'根据给定查询语句,生成最终的查询语句(只取当前页内容):适用于oracle数据库
XD_SQL=" select * from (select rownum r_n,temptable.* from ("
XD_SQL=XD_SQL&str_sql
XD_SQL=XD_SQL&" ) temptable ) where r_n between " & cstr((int_curpage -1) * XD_PageSize +1) & " and " & cstr(int_curpage * XD_PageSize)
'查询总记录数的查询语句
XD_Count_SQL="select count(*) from ("& str_sql & ")"
end if
End Property
'================================================================
'GetSubmitForm属性 设置查询条件的表单
'
'================================================================
Public Property Let GetSubmitForm(frmName)
SubmitForm=trim(frmName)
End Property
'================================================================
'GetSubmitForm1方法 输出分页导航所需脚本
'
'================================================================
public sub GetSubmitForm1()
'页导航的javascript函数
Response.Write " "+vrcrlf
Response.Write ("<Script language=""javascript"">") +vbcrlf
Response.Write " function generalSubmit(i)"+vbcrlf
Response.Write " {"+vbcrlf
Response.Write " document."&SubmitForm&".flag.value=""query1111111155555"";"+vbcrlf
Response.Write " document."&SubmitForm&".currentpage.value=i;"+vbcrlf
Response.Write " "&SubmitForm&".submit();"+vbcrlf
Response.Write " }"+vbcrlf
'改变页大小的javascript函数
Response.Write " function changePageSize(ii)"+vbcrlf
Response.Write " {"+vbcrlf
Response.Write " document."&SubmitForm&".flag.value=""query1111111155555"";"+vbcrlf
Response.Write " document."&SubmitForm&".currentpage.value=1;"+vbcrlf
Response.Write " document."&SubmitForm&".pagesize.value=ii;"+vbcrlf
Response.Write " "&SubmitForm&".submit();"+vbcrlf
Response.Write " }"+vbcrlf
Response.Write ("</Script>")+vbcrlf
Response.Write " "+vrcrlf
end sub
'==================================================================
'totalRecordCount 属性
'关于记录总数
'
'==================================================================
Public Property Let totalRecordCount(int_totalRecordCount)
If IsNumeric(int_totalRecordCount) Then
int_totalRecord=CLng(int_totalRecordCount)
End If
End Property
Public Property Get totalRecordCount
If not(int_totalRecord="" or (not(IsNumeric(int_totalRecord)))) Then
totalRecordCount=int_totalRecord
End If
End Property
'==================================================================
'GetRecordCount 方法
'返回当前记录数
'
'==================================================================
public function GetRecordCount()
GetRecordCount=int_totalRecord
end function
'==================================================================
'Class_Initialize 类的初始化
'初始化当前页的值
'
'==================================================================
Private Sub Class_Initialize
'========================
'设定一些参数的黙认值
'========================
' XD_PageSize=10 '设定分页的默认值为10
'========================
'获取当前面的值
'========================
If Request("currentpage")="" Then
int_curpage=1
ElseIf not(IsNumeric(Request("currentpage"))) Then
int_curpage=1
ElseIf CInt(Trim(Request("currentpage")))<1 Then
int_curpage=1
Else
Int_curpage=CInt(Trim(Request("currentpage")))
End If
End Sub
'=============================================
'ShowPage 创建分页导航条
'有首页、前一页、下一页、末页、还有数字导航
'
'=============================================
Public Sub ShowPage()
Dim str_tmp
XD_sURL = GetUrl()
' 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
'modify by wls 041215 For the right pages display---------------
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 " "
ShowGoto
End Sub
'=============================================
'ShowFirstPrv 显示首页、前一页
'
'
'=============================================
Private Function ShowFirstPrv()
Dim Str_tmp,int_prvpage
If int_curpage=1 Then
str_tmp=Btn_First&" "&Btn_Prev
Elseif int_curpage=0 then
str_tmp=Btn_First&" "&Btn_Prev
else
int_prvpage=int_curpage-1
str_tmp="<a href=""#"" onclick=""javascript:generalSubmit('1')"" alt=""第一页"">" & Btn_First&"</a> <a href=""#"" onclick=""javascript:generalSubmit('"&int_prvpage&"')"" alt=""前一页"">" & 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 & " " & Btn_Last
Else
Int_NextPage=int_curpage+1
str_tmp="<a href=""#"" onclick=""javascript:generalSubmit('"&int_nextpage&"')"" alt=""后一页"">" & Btn_Next&"</a> <a href=""#"" onclick=""javascript:generalSubmit('"&int_totalpage&"')"" alt=""最后一页"">" & Btn_Last&"</a>"
End If
ShowNextLast=str_tmp
End Function
'End Function
'=============================================
'ShowNumBtn 修改后的数字导航
'
'=============================================
Function showNumBtn()
Dim i,str_tmp,end_page,start_page
start_page=1
'add by sll 2005.05.20 int_curpage=0
if int_curpage=0 then
str_tmp=str_tmp&"0"
else
if int_curpage>1 then
start_page=int_curpage
if (int_curpage<=5) then
start_page=1
end if
if (int_curpage>5) then
start_page=int_curpage-2
end if
end if
end_page=start_page+5
if end_page>int_totalpage then
end_page=int_totalpage
end if
For i=start_page to end_page
strTemp=XD_sURL & CStr(i)
str_tmp=str_tmp & "[<a href=""#"" onclick=""javascript:generalSubmit('"&i&"')"">"&i&"</a>] "
Next
end if
showNumBtn=str_tmp
End Function
'=============================================
'ShowGoto 页面跳转
'页面自动跳转
'add by sll 2005.05.20
'=============================================
Private Function ShowGoto()
'response.write int_totalPage
dim inti
if int_totalPage<=0 then
response.write "<select name='goto' disabled>"
Response.Write "<option value='0'>0</option>"
response.write "</select>"
else
response.write "<select name='goto' >"
for inti=1 to int_totalPage
Response.Write "<option value='"&inti&"'"
if cstr(inti)=cstr(int_curpage) then
response.write "selected"
end if
response.write" >"&inti&"</option>"
next
response.write "</select>"
end if
End Function
'=============================================
'ShowPageInfo 分页信息
'根据要求自行修改
'
'=============================================
Private Function ShowPageInfo()
Dim str_tmp
str_tmp=" [页次:<font color=red>"&int_curpage&"</font>/"&int_totalpage&"页] [共"&int_totalrecord&"条] ["&XD_PageSize&"条/页]"
ShowPageInfo=str_tmp
End Function
'=============================================
'ShowPageSizeChange 改变页大小
'根据要求自行修改
'
'=============================================
public sub ShowPageSizeChange()
Dim str_tmp
str_tmp="页大小:<select name='sssssPageSize' >"
str_tmp=str_tmp & "<option"
if XD_PageSize=10 then str_tmp =str_tmp & " selected "
str_tmp=str_tmp & " value='10'>10</option>"
str_tmp=str_tmp & "<option"
if XD_PageSize=20 then str_tmp =str_tmp & " selected "
str_tmp=str_tmp & " value='20'>20</option>"
str_tmp=str_tmp & "<option"
if XD_PageSize=50 then str_tmp =str_tmp & " selected "
str_tmp=str_tmp & " value='50'>50</option>"
str_tmp=str_tmp & "<option"
if XD_PageSize=int_totalRecord then str_tmp =str_tmp & " selected "
str_tmp=str_tmp & " value='" & int_totalRecord & "'>all</option>"
str_tmp=str_tmp & "</select>"
response.Write str_tmp
End sub
'=============================================
'修改后的获取当前Url参数的函数
'Codeing by Redsun
'northsnow注释:不知道用在何处,但是保留
'=============================================
Private Function GetUrl()
Dim ScriptAddress, M_ItemUrl, M_item
ScriptAddress = CStr(Request.ServerVariables("SCRIPT_NAME"))&"?" '取得当前地址
If (Request.QueryString <> "") Then
M_ItemUrl = ""
For Each M_item In Request.QueryString
If InStr("page",M_Item)=0 Then
M_ItemUrl = M_ItemUrl & M_Item &"="& Server.URLEncode(Request.QueryString(""&M_Item&"")) & "&"
End If
Next
ScriptAddress = ScriptAddress & M_ItemUrl '取得带参数地址
End If
GetUrl = ScriptAddress '& "page="
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("" & SW_Error & "")
Response.End
End If
End Sub
End class
% >