先将分页类源代码发上来,随后有空会发一些实例来讲解其用法,以及如何扩展其功能。
类原代码如下:
< %
==============================================
xdownpage asp版本
当前版本:2.0
原版本 1.00
code by zykj2000
email: zykj_2000@163.net
bbs: http://bbs.513soft.net
升级版本:1.5 (asp + oracle)
updated by doublel
email:
blog: ttp://blog.csdn.net/doublel/
升级说明:
升级版本:2.0 (asp + oracle) —->当前版本
保留原名:xdownpage
updated by northsnow
email: northsnow@163.com
blog: http://blog.csdn.net/precipitant
升级说明:
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 onchange=javascript:generalsubmit(this.value)>”
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 onchange=changepagesize(this.value)>”
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
% >