asp分页的基于对象的解决

来源:岁月联盟 编辑:zhuzhu 时间:2003-07-25
论坛上有不少的关于asp分页的文章,基本思想是一致的,我把这些网友的思路深化了一下,用javaScript套上了一层基于对象的外衣,这样调用起来就不显得乱了,其它的话就不多说了,详见程序中的注释。

下边是源程序:
<script language=javascript runat=server>
function trim(str)//去掉字符串的首尾空格
{ var tmpStr=new String(str)
return tmpStr.replace(/(^/s*)|(/s*$)/g, "");
}
</script>


<SCRIPT LANGUAGE=javascript RUNAT=Server>
// ************************************************************************
// Script Compont Object Model
// Design for Active Server Pages
//
// Copyright 2003 Version 1.0
// Made by yinShuGuang
// All Rights Reserved.
// ************************************************************************

function JPageNavbar(){
_LB__Prototype();
}
function _LB__Prototype()
{

// public members
JPageNavbar.prototype.PageSize="0"
JPageNavbar.prototype.RecordCount="0" //总记录数
JPageNavbar.prototype.PageCount="1" //总页数
JPageNavbar.prototype.CurrentPage="1"

JPageNavbar.prototype.PnWidth="100%"
JPageNavbar.prototype.PnAlign="right"
JPageNavbar.prototype.PlWidth="100%" //表格宽度
JPageNavbar.prototype.PlAlign="right" // 表格的对齐方式

// private members

//public methods
JPageNavbar.prototype.getCurrentPage=_getCurrentPage
JPageNavbar.prototype.pnDisplay = _PN_show;
JPageNavbar.prototype.plDisplay = _PL_show;

//private methods


//scope implementation in _LB__Prototype function

function _getCurrentPage(){
var pageNo=new String() //当前显示的是第几页
//取得当前页
pageNo = Request.QueryString ("PageNo")
pageNo=fTrim(pageNo)
//如果没有选择第几页,则默认显示第一页;
if (pageNo=="") {
pageNo = 1
}
this.CurrentPage=pageNo
return pageNo
}
function _PL_show(){

var strBuilder=new String()
strBuilder=""
var p=(this.CurrentPage-(this.CurrentPage%10))/10 //计算分页显示的页数
//首组为第0族

strBuilder+="<table border=/"0/" cellpadding=/"0/" cellspacing=/"0/" "
strBuilder+=" width=/""+this.PlWidth+"/" align=/""+this.PlAlign+"/">"

strBuilder+=" <tr> "
strBuilder+=" <td valign=/"middle/" align=/"right/">分页:"

if (this.CurrentPage==1){
strBuilder+="<font face=/"webdings/" color=/"#ff0000/">9</font> "
}
else{
strBuilder+="<a href=/"?PageNo=1/" title=/"首页/"><font face=/"webdings/">9</font></a>

"
}
//上十页
if (p*10>0){
strBuilder+="<a href=/"?PageNo="+(p*10)+"/" title=上十页><font

face=/"webdings/">7</font></a> "
}
strBuilder+="<b>"
//分页列表
for(var i=p*10+1;i<=p*10+10;i++){
if (i==this.CurrentPage){
strBuilder+="<font color=/"#000000/">"+i+"</font> "
}
else{
strBuilder+="<a href=?PageNo="+i+" title=/"转到: 第"+i+"页/">"+i+"</a> "
}
if (i>=this.PageCount) break;
}
strBuilder+= "</b>"
//显示下十页
if (i<this.PageCount){
strBuilder+="<a href=/"?PageNo="+i+"/" title=/"下十页/"><font

face=/"webdings/">8</font></a> "
}
//显示尾页
if (this.CurrentPage==this.PageCount){
strBuilder+= "<font face=/"webdings/" color=/"#000000/">:</font> "
}
else{
strBuilder+= "<a href=?PageNo="+this.PageCount+" title=/"尾页/"><font

face=/"webdings/">:</font></a> "
}
strBuilder+= "</td></tr></table>"
Response.Write(strBuilder)
}
function _PN_show(){
var strBuilder=new String()
var nextPageNo
strBuilder=""
strBuilder+="<table border=/"0/" cellpadding=/"0/" cellspacing=/"0/" "
strBuilder+=" width=/""+this.PnWidth+"/" align=/""+this.PnAlign+"/">"
strBuilder+="<tr>"
strBuilder+="<td valign=/"middle/">页次:

[<b>"+this.CurrentPage+"</b>/<b>"+this.PageCount+"</b>]页 每页[<b>"+this.PageSize+"</b>]条 总记录

数:[<b>"+this.RecordCount+"</b>]条</td>"
strBuilder+="<td align=/"right/">"
if (this.CurrentPage>1){
nextPageNo=this.CurrentPage
nextPageNo--
strBuilder+="[<a href=?pageNo="+nextPageNo+" title=/"转到上一页/">上一页</a>]"
}
if (this.CurrentPage<this.PageCount){
nextPageNo=this.CurrentPage
nextPageNo++
strBuilder+="[<a href=?pageNo="+nextPageNo+" title=/"转到下一页/">下一页</a>]"
}
strBuilder+="</td></tr></table>"
Response.Write(strBuilder)
}
}
</SCRIPT>


在调用这个对象之时,注意保存sql语句的查询条件下面我的解决方法:
<@language=JavaScript>
var tj=trim(Request.Form("tj"))
//或 var tj=trim(Request.QueryString("tj"))
if (tj==""){
//表明不是从查询页传过来的条件,而是从分页导向过来
tj=trim(Session("tj"))
}
else{ //注意保存条件
Session("tj")=tj
}

这仅是一个基本的解决思路,遇到实际情况您可要具体问题具体分析
下边是对象的调用过程

//Ado.RecordSet记录分页对象
//设置分页
var RowCount =3
var fy=new JPageNavbar() //创建对象
if (!rsRpt.Eof){
rsRpt.PageSize = RowCount //设置数据集的页记录
fy.PageSize=RowCount
rsRpt.AbsolutePage =fy.getCurrentPage()
fy.RecordCount=rsRpt.RecordCount
fy.PageCount=rsRpt.pageCount
}


//显示分页
<%fy.pnDisplay() //这是一个具有上一页,下一页方式的导航%>
<%fy.plDisplay() // 这是一个具有页码列表的导航%>