先将分页类源代码发上来,随后有空会发一些实例来讲解其用法,以及如何扩展其功能。
类原代码如下:
< %
'==============================================
'XDOWNPAGE ASP版本
'当前版本:2.0
'
'
'原版本 1.00
'Code by zykj2000
'Email: zy***[email protected]
'程序方法说明
'ShowPage 显示分页导航条,唯一的公用方法
'ShowPageSizeChange() 显示改变页大小的列表
'
'例:
'
' '包含文件
'
' Set mypage=new xdownpage '创建对象
' my***e.getconn=conn '得到数据库连接
' my***e.getsql="select * from productinfo order by id asc"
' my***e.pagesize=5 '设置每一页的记录条数据为5条
'my***e.totalRecordCount=rsTotalCount 设置总记录数
' set rs***page.getrs() '返回Recordset
'my***e.GetSubmitForm="frmQuery" ' 分页默认提交的表单,currentpage参数
'Re***nse.write(my***e.GetSubmitForm1()) '输出分页提交的函数
' my***e.showpage() '显示分页信息,这个方法可以,在set rs***page.getrs()以后
' 任意位置调用,可以调用多次
' do while not rs***f '接下来的操作就和操作一个普通Recordset对象一样操作
' re***nse.write rs(0) & "
' " '这里就可以自定义显示方式了
' rs***venext
' loop
'
'添加了保存当前页面数量的提交脚本
'函数为GetSubmitForm()
'需要提交给函数GetSubmitForm一个表单名字
'在这个提交的表单里面保存变量flag,currentpage,pagesize,rsTotalCount 四个参数
'例子如下
'flag=request("flag")
'currentpage=request("currentpage")
'currentpage=request("pagesize")
'currentpage=request("rsTotalCount")
'在提交的表单里面加入下面四个input
'
'
'
'
'==============================================
Const Btn_First="9" '定义第一页按钮显示样式
Const Btn_Prev="3" '定义前一页按钮显示样式
Const Btn_Next="4" '定义下一页按钮显示样式
Const Btn_Last=":" '定义最后一页按钮显示样式
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***=Server.createobject("ad***.recordset")
'XD***.PageSize=PageSize
XD***.CursorLocation=3
XD***.Open XD_SQL,XD_Conn,3,1
in***urcount=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 er***umber<>0 then
Re***nse.Write er***lear
end if
Set GetRs=XD_RS
End Property
'=================================================================
'queryRSCount 方法
'查询总记录数
'=================================================================
Public sub queryRsCount()
'下面代码用于计算总记录数
if XD_Count_SQL<>"" then
set rs***lcount=server.createobject("ad***.recordset")
rs***lcount.CursorLocation=3
rs***lcount.open XD_Count_SQL,conn,3,1
if (rs***lcount.eof and rs***lcount.bof) then
int_totalRecord=0
else
int_totalRecord=rs_sqlcount(0)
int_totalRecord=clng(int_totalRecord)
end if
rs***lcount.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函数
Re***nse.Write " "+vrcrlf
Re***nse.Write ("")+vbcrlf
Re***nse.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()
' in***otalRecord=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
'========================================================
'显示分页信息,各个模块根据自己要求更改显求位置
'========================================================
're***nse.write " "
str_tmp=ShowFirstPrv
re***nse.write str_tmp
str_tmp=showNumBtn
re***nse.write str_tmp
str_tmp=ShowNextLast
re***nse.write str_tmp
str_tmp=ShowPageInfo
re***nse.write str_tmp
Re***nse.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="" & Btn_First&" " & Btn_Prev&""
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="" & Btn_Next&" " & Btn_Last&""
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 & "["&i&"] "
Next
end if
showNumBtn=str_tmp
End Function
'=============================================
'ShowGoto 页面跳转
'页面自动跳转
'add by sll 2005.05.20
'=============================================
Private Function ShowGoto()
're***nse.write int_totalPage
dim inti
if int_totalPage<=0 then
re***nse.write ""
else
re***nse.write "
for inti=1 to int_totalPage
Re***nse.Write ""
next
re***nse.write ""
end if
End Function
'=============================================
'ShowPageInfo 分页信息
'根据要求自行修改
'
'=============================================
Private Function ShowPageInfo()
Dim str_tmp
str_tmp=" [页次:"&int_curpage&"/"&int_totalpage&"页] [共"&int_totalrecord&"条] ["&XD_PageSize&"条/页]"
ShowPageInfo=str_tmp
End Function
'=============================================
'ShowPageSizeChange 改变页大小
'根据要求自行修改
'
'=============================================
public sub ShowPageSizeChange()
Dim str_tmp
str_tmp="页大小:"
re***nse.Write str_tmp
End sub
'=============================================
'修改后的获取当前Url参数的函数
'Codeing by Redsun
'northsnow注释:不知道用在何处,但是保留
'=============================================
Private Function GetUrl()
Dim ScriptAddress, M_ItemUrl, M_item
ScriptAddress = CStr(Re***st.ServerVariables("SCRIPT_NAME"))&"?" '取得当前地址
If (Re***st.QueryString <> "") Then
M_ItemUrl = ""
For Each M_item In Re***st.QueryString
If InStr("page",M_Item)=0 Then
M_ItemUrl = M_ItemUrl & M_Item &"="& Se***r.URLEncode(Re***st.QueryString(""&M_Item&"")) & "&"
End If
Next
ScriptAddress = ScriptAddress & M_ItemUrl '取得带参数地址
End If
GetUrl = ScriptAddress '& "page="
End Function
'=============================================
' 设置 Terminate 事件。
'=============================================
Private Sub Class_Terminate
'XD***.close
'Set XD_RS=nothing
End Sub
'=============================================
'ShowError 错误提示
'=============================================
Private Sub ShowError()
If str_Error <> "" Then
Re***nse.Write("" & SW_Error & "")
Re***nse.End
End If
End Sub
End class
% >