一个asp翻页类,改进版

Pager.asp

<script language="vbscript" runat="server">
Class Pager
'------------------------------------------------------
'名称: Pager 类(V1.01)
'功能: 搜索数据信息处理,具有翻页功能
'作者: 刘光涛[Gunter Liu][liugt_2003@163.com][QQ:370878247]
'日期: 2007-7-12
'使用请保留作者信息,谢谢!     
'------------------------------------------------------
'//-------------------- 定义变量 --------------------//'
    Private strTableName        '//查询主表表名
    Private strCountSql         '//生成查询总数sql语句
    Private strRecordSql        '//生成记录集sql语句
    Private arrCondition()      '//查询条件数组
    Private strCondition        '//查询条件字符串
    Private intPosition         '//指针位置(查询起始位置)
    Private intPage             '//当前页
    Private intPageSize         '//每页记录数
    Private intPageCount        '//总页数
    Private intRecordCount      '//查询总记录数
    Private strField            '//输出的字段字符串
    Private strOrder            '//排序字符串
    Private strPk               '//主键,预留值
    Private objConn             '//连接对象
    Private strClassName        '//类名
    Private strVersion          '/版本号
    Private blnHaltErr          '//是否停止程序执行
    Private blnShowErr          '//是否显示错误详情
    Private strLastError        '//最后出错地方
'//-------------------- 事件、方法 --------------------//'
    '//类初始化事件和变量
    Private Sub Class_Initialize()
        ReDim arrCondition(-1)
        strTableName        =""
        strCountSql         =""
        strRecordSql        =""
        strCondition        =""
        intPosition         =0
        intPage             =1
        intPageSize         =20
        intPageCount        =0
        intRecordCount      =0
        strField            ="*"
        strOrder            =""
        strPk               =""
        objConn             =Null
        strClassName        ="通用翻页类Pager V1.01"
        strVersion          ="Pager V1.01"
        blnHaltErr          =True
        blnShowErr          =False
        strLastError        =""
    End Sub
    '//类结束事件,清理内存
    Private Sub Class_Terminate()
        If isObject(objConn) Then Set objConn = Nothing
    End Sub
'----------------------------------------//对外实现方法
     '//添加条件
    Public Sub addCondition(byVal str)
        If Len(str) < 3 Then Exit Sub
        ReDim Preserve arrCondition(UBound(arrCondition)+1)
        arrCondition(UBound(arrCondition)) = str
    End Sub
    '//版本信息
    Public Function getVersion()
        getVersion=strVersion
    End Function

'----------------------------------------//输入属性
    '//定义连接对象
    Public Property Set ActiveConnection(byVal obj)
        If not isObject(obj) Then
            ShowErr "ActiveConnection属性出错:没有发现数据库链接对象"
        End If
        Set objConn = obj
    End Property
    '//定义查询表名
    Public Property Let TableName(byVal tbl)
        If len(tbl)<1 Then
            ShowErr "TableName属性:必须指定数据库表名称"
        End If
        strTableName = tbl
    End Property
    '//定义输出的字段名
    Public Property Let Fields(byVal fld)
        If len(fld)<1 Then Exit Property
        strField = fld
    End Property
    '//定义排序规则
    Public Property Let OrderBy(byVal odr)
        If len(odr)<1 Then Exit Property
        strOrder = odr
    End Property
    '//定义主键,为以后预留,现在用不上
    Public Property Let PKey(byVal key)
        If len(key)<1 Then
            ShowErr "TPKey属性出错:主键值不能设置为空"
        End If
        strPk = key
    End Property
    '//定义查询条件
    Public Property Let Condition(byVal cdtion)
        If len(cdtion)<3 Then Exit Property
        Call addCondition(cdtion)
    End Property
    '//定义每页的记录条数
    Public Property Let PageSize(byVal ps)
        If len(ps)<1 Then Exit Property
        If isNumeric(ps) Then
            If CLng(ps) >= 0 Then intPageSize = CLng(ps)
        End If
    End Property
    '//定义当前页码
    Public Property Let Page(byVal p)
        If len(p)<1 Then Exit Property
        If isNumeric(p) Then
            Call getPageCount()
            p=CLng(p)
            If intPageCount>0 And p>0 And p<=intPageCount Then
                intPage = p
            ElseIf p>intPageCount Then
                intPage = intPageCount
            ElseIf p<=0 Then
                intPage = 1
            End If
        End If
        intPosition = (intPage-1) * intPageSize
    End Property
    '//定义当前页码
    Public Property Let AbsolutePage(byVal p)
        If len(p)<1 Then Exit Property
        If isNumeric(p) Then
            Call getPageCount()
            p=CLng(p)
            If intPageCount>0 And p>0 And p<=intPageCount Then
                intPage = p
            ElseIf p>intPageCount Then
                intPage = intPageCount
            ElseIf p<=0 Then
                intPage = 1
            End If
        End If
        intPosition = (intPage-1) * intPageSize
    End Property
    '//定义当前页数据指针开始位置
    Public Property Let StartPosition(byVal p)
        If len(p)<1 Then Exit Property
        If isNumeric(p) Then
            Call getPageCount()
            p=CLng(p)
            If intPageCount>0 And p>=0 And p<intRecordCount Then
                intPosition = p
            ElseIf p>intRecordCount Then
                intPosition = 0
            ElseIf p<0 Then
                intPosition = 0
            End If
        End If
        intPage=Abs(Int((intPosition+1)/intPageSize*(-1)))
    End Property
    '//定义当前页数据指针开始位置
    Public Property Let Position(byVal p)
        If len(p)<1 Then Exit Property
        If isNumeric(p) Then
            Call getPageCount()
            p=CLng(p)
            If intPageCount>0 And p>=0 And p<intRecordCount Then
                intPosition = p
            ElseIf p>intRecordCount Then
                intPosition = 0
            ElseIf p<0 Then
                intPosition = 0
            End If
        End If
        intPage=Abs(Int((intPosition+1)/intPageSize*(-1)))
    End Property
    '//调试时候是否显示报错
    Public Property Let ShowError(byVal err)
        blnShowErr=err
    End Property
    '//出错后是否立即停止程序执行
    Public Property Let HaltError(byVal err)
        blnHaltError=err
    End Property
   
'-------------------------------------------//输出属性
    '//取得当前页码
    Public Property Get Page
        Page = intPage
    End Property
    '//取得当前页码
    Public Property Get AbsolutePage
        AbsolutePage = intPage
    End Property
    '//取得当前页起始指针位置
    Public Property Get StartPosition
        StartPosition = intPosition
    End Property
    '//取得当前页起始指针位置
    Public Property Get Position
        Position = intPosition
    End Property
    '//取得当前查询的条件
    Public Property Get Condition
        If Len(strCondition)<1 Then Call getCondition()
        Condition = strCondition
    End Property
    '//取得总的记录数
    Public Property Get RecordCount
        If intRecordCount<1 Then Call getRecordCount()
        RecordCount = intRecordCount
    End Property
    '//取得总页数
    Public Property Get PageCount
        If intPageCount<1 Then Call getPageCount()
        PageCount = intPageCount
    End Property
    '//取得每页定义记录数
    Public Property Get PageSize
        PageSize = intPageSize
    End Property
    '//取得页实际显示条数
    '//最后可以得到页的结束记录数=Position+ShowSize
    Public Property Get ShowSize
        If intPage = intPageCount Then
            showSize = intRecordCount-intPosition+1
        ElseIf intPageCount <= 1 Then
            showSize = intRecordCount
        Else
            showSize = intPageSize
        End If
    End Property
    '//得到分页后的记录集
    Public Property Get RecordSet
        On Error Resume Next
        Call getSql()
        Set RecordSet = objConn.Execute(strRecordSql, 0, 1)
        If Err Then
            ShowErr "Recordset属性出错:请检查提取数据SQL语句是否正确<br/>提取数据SQL:" & strRecordSql
            set Recordset = null
        End If
    End Property
    '//得到记录集数组
    Public Property Get RecordArray
        On Error Resume Next
        Call getSql()
        RecordArray = objConn.Execute(strRecordSql, 0, 1).getRows()
        If Err Then
            ShowErr "RecordArray属性出错:SQL语句有错或者记录集为空!<br/>提取数据SQL:" & strRecordSql
            RecordArray = Array(-1)
        End If
    End Property
    '//版本信息
    Public Property Get Version
        Version = strVersion
    End Property
    '//得到最后出错信息
    Public Property Get LastError
        LastError = strLastError
    End Property
   
'---------------------------------------------------//类内部实现方法
    '//输入数据到页面,不带任何修饰
    Private Sub out(byVal p)
        Response.Write p
    End Sub
    '//显示提示信息
    Private Sub ShowErr(byVal strErr)
            err.Clear
            strLastError = "<p><font color=red style='font-size;14px'><b>搜索类异常:</b></font><br /><font style='font-size;9px'>" & strErr & "</font></p>"
            If blnShowErr Then out strLastError
            If blnHaltErr Then response.End
    End Sub
    '//得到分页的SQL语句(记录总数和记录集)
    Private Sub getSql()
        If len(strCountSql)>0 And len(strRecordSql)>0 Then Exit Sub
        If len(strTableName)<1 Then
            ShowErr "getSql函数出错:必须制定表名称TableName"
        End If
        If len(strOrder)<1 Then
            ShowErr "getSql函数出错:必须得指定至少一个排序规则字段OrderBy"
        End If
        If not isObject(objConn) Then
            ShowErr "getSql函数出错:没有发现数据库链接对象ActiveConnection"
        End If
        Call getCondition()
        strCountSql="SELECT COUNT(*) FROM " & strTableName & strCondition
        strRecordSql="SELECT * FROM (SELECT TOP "&intPageSize&" * FROM (SELECT TOP "&intPosition + intPageSize&" "&strField&" FROM "&strTableName&strCondition&" ORDER BY "&strOrder&" DESC) DERIVEDTBL ORDER BY "&strOrder&") DERIVEDTBL ORDER BY "&strOrder&" DESC"
    End Sub
    '//产生条件字符串
    Private Sub getCondition()
        If len(strCondition)<1 And UBound(arrCondition)>-1 Then
            strCondition = " WHERE " & Join(arrCondition," AND ")
        End If
    End Sub
    '//得到总记录数
    Private Sub getRecordCount()
        On Error Resume Next
        Dim intCount
        Call getSql()
        intCount = objConn.Execute(strCountSql, 0, 1)(0)
        If Err Then
            ShowErr "getRecordCount函数出错:请检查计算记录SQL语句<br/>记录总数SQL:"&strCountSql
        End If
        intRecordCount = intCount
    End Sub
    '//计算总页数
    Private Sub getPageCount()
        If intRecordCount < 1 Then Call getRecordCount()
        If intRecordCount < 1 Then Exit Sub
        intPageCount = Abs(Int(0-(intRecordCount/intPageSize)))
    End Sub
End Class
</script>

具体用法:
search.asp

<!--#include file="Function/Pager.asp"-->
<%
'//-----------------查询字符串处理-------------------------//'
'----------------------------------------//去掉多余查询
dim strQueryString  : strQueryString=filtQuery("startposition")
'----------------------------------------//得到所有查询条件值
dim intRentSale         :intRentSale        =Query("rad_int_tradetype", 1)'//交易类型(租、售),居然发现"rdo_int_tradetype"变量不能获得值,原来变量名称不能用rdo开头,有点怪!--2007-7-24
dim intUnits            :intUnits           =Query("lst_int_units", 1)'//租价单位
dim intDistrict         :intDistrict        =Query("lst_int_distict", 1)'//行政区域
dim intMinArea          :intMinArea         =Query("lst_int_minarea", 1)'//面积
dim intMaxArea          :intMaxArea         =Query("lst_int_maxarea", 1)
dim intMinPrice         :intMinPrice        =Query("lst_int_minprice", 1)'//价格
dim intMaxPrice         :intMaxPrice        =Query("lst_int_maxprice", 1)
dim strFace             :strFace            =Query("lst_str_face", 0)'//朝向
dim strFitment          :strFitment         =Query("lst_str_fitment", 0)'//装修
dim strNameKeyWord      :strNameKeyWord     =Query("txt_str_keywords", 0)'//关键字
dim intStartPosition    :intStartPosition   =Query("start", 1)'//提取数据起始位置
'dim intOrderBy         :intOrderBy         =Query("orderby", 1)'//数据列表排序规则

'//----------------获得数据,产生RecordSet对象-------------//'
'----------------------------------------//产生对象,初始化实例
dim objPage :set objPage                = new Pager
set objPage.ActiveConnection            = conn'//数据库连接对象
'objPage.ShowError                       = true
objPage.TableName                       = "tbl_RE_OwnerOfficeInfo,tbl_PL_District"'//表名称
objPage.Fields                          = "OO_ID_I,D_Name_VC,OO_Name_VC,OO_RentPrice_D,OO_SalePrice_D,OO_BuildArea_D,OO_CreationTime_DT,OO_HtmPath_VC"'输出字段
objPage.Condition                       = "tbl_RE_OwnerOfficeInfo.FK_District_I=tbl_PL_District.D_ID_I"'//外键关系
objPage.OrderBy                         = "OO_ID_I"'//排序字段,最好是主键
objPage.PageSize                        = 20'//本页显示个数
objPage.StartPosition                   = intPosition
'----------------------------------------//生成查询条件
'//添加区域
If intDistrict>0 Then objPage.addCondition "FK_District_I="&intDistrict
'//添加面积
If intMinArea>0 Then objPage.addCondition "OO_BuildArea_D>="&intMinArea
If intMaxArea>0 Then objPage.addCondition "OO_BuildArea_D<="&intMaxArea
'//添加价格
If intRentSale=1 Then
'-------------//租
If intUnits=1 Then'-----------//元/平/天--基本价
If intMinPrice>0 Then objPage.addCondition "OO_RentPrice_D>="&intMinPrice
If intMaxPrice>0 Then objPage.addCondition "OO_RentPrice_D<="&intMaxPrice
ElseIf intUnits=2 Then'-------//元/月,预留字段:OO_TotalRentPrice_D月总价
If intMinPrice>0 Then objPage.addCondition "(OO_RentPrice_D*30*OO_BuildArea_D)>="&intMinPrice
If intMaxPrice>0 Then objPage.addCondition "(OO_RentPrice_D*30*OO_BuildArea_D)<="&intMaxPrice
End If
ElseIf intRentSale=2 Then
'-------------//售
'------//基本售价
If intMinPrice>0 Then objPage.addCondition "OO_SalePrice_D>="&intMinPrice
If intMaxPrice>0 Then objPage.addCondition "OO_SalePrice_D<="&intMaxPrice
'------//总价
'后面补充
End If
'//添加朝向
If strFace<>"" Then objPage.addCondition "OO_Face_VC LIKE '"&strFace&"%'"
'//添加装修
If strFitment<>"" Then objPage.addCondition "OO_Fitment_VC LIKE '"&strFitment&"%'"
'//添加关键字
If strNameKeyWord<>"" Then objPage.addCondition "OO_Name_VC LIKE '"&strNameKeyWord&"'"
'----------------------------------------//产生数据
'objPage.StartPosition
dim intPageSize     :intPageSize        = objPage.PageSize
dim intRecordCount  :intRecordCount     = objPage.RecordCount
dim intPage         :intPage            = objPage.AbsolutePage
dim intPageCount    :intPageCount       = objPage.PageCount
dim strPageInfo     :strPageInfo        = "本次搜索到共"&intRecordCount&"个 第"&intPage&"/"&intPageCount&"页"
If intPageCount <= 1 Then
    strPageInfo = strPageInfo & " 首页 | 上一页 | 下一页 | 尾页 "
ElseIf intPage <= 1 Then
    strPageInfo = strPageInfo & " 首页 | 上一页 | <a href=""?"&strQueryString&"startposition="&intStartPosition+intPageSize&""">下一页</a> | <a href=""?"&strQueryString&"startposition="&(intPageCount-1)*intPageSize&""">尾页</a> "
ElseIf intPage > 1 And intPage < intPageCount Then
    strPageInfo = strPageInfo & " <a href=""?"&strQueryString&"startposition=0"">首页</a> | <a href=""?"&strQueryString&"startposition="&intStartPosition-intPageSize&""">上一页</a> | <a href=""?"&strQueryString&"startposition="&intStartPosition+intPageSize&""">下一页</a> | <a href=""?"&strQueryString&"startposition="&(intPageCount-1)*intPageSize&""">尾页</a> "
ElseIf intPage >= intPageCount Then
    strPageInfo = strPageInfo & " <a href=""?"&strQueryString&"startposition=0"">首页</a> | <a href=""?"&strQueryString&"startposition="&intStartPosition-intPageSize&""">上一页</a> | 下一页 | 尾页 "
End If
'//----------------------显示数据列表--------------------//'
dim objRecordSet    :set objRecordSet   = objPage.RecordSet
dim tdCls           :tdCls              = "td1"
dim blnCls          :blnCls             = false
dim strRecordListHtml         :strRecordListHtml            = ""'//输出列表代码
do while 0=0
    if objRecordSet.Eof then exit do
    if blnCls  then
        tdCls = "td2"
        blnCls = false
    else
        tdCls = "td1"
        blnCls = true
    end if
    strRecordListHtml = strRecordListHtml & "<tr title="""&objRecordSet("OO_Name_VC")&""">"&vbCRLF
    strRecordListHtml = strRecordListHtml & "<td class="""&tdCls&""" align=""left""><a href="""&objRecordSet("OO_HtmPath_VC")&""" title="""&objRecordSet("OO_Name_VC")&""" target=""_blank"">"&objRecordSet("OO_Name_VC")&"</a></td>"
    strRecordListHtml = strRecordListHtml &  "<td class="""&tdCls&""" align=""center"">"&objRecordSet("D_Name_VC")&"</td>"
    strRecordListHtml = strRecordListHtml &  "<td class="""&tdCls&""" align=""left"">"&objRecordSet("OO_BuildArea_D")&"平</td>"
    strRecordListHtml = strRecordListHtml &  "<td class="""&tdCls&""" align=""left"">"
    If isNumeric(objRecordSet("OO_RentPrice_D")) Then If CSng(objRecordSet("OO_RentPrice_D"))>0 then strRecordListHtml = strRecordListHtml &  "租:"&objRecordSet("OO_RentPrice_D") & "元/平/天<br/>"
    If isNumeric(objRecordSet("OO_SalePrice_D")) Then If CSng(objRecordSet("OO_SalePrice_D"))>0 then strRecordListHtml = strRecordListHtml &  "售:"&objRecordSet("OO_SalePrice_D") & "万元"
    strRecordListHtml = strRecordListHtml &  "</td>"
    strRecordListHtml = strRecordListHtml & "<td class="""&tdCls&""" align=""center"">"&objRecordSet("OO_CreationTime_DT")&"</td>"&vbCRLF
    strRecordListHtml = strRecordListHtml & "</tr>"&vbCRLF
    objRecordSet.movenext
loop
    strRecordListHtml = strRecordListHtml & "<tr>"&vbCRLF
    strRecordListHtml = strRecordListHtml & "<td height=""20"" colspan=""5"" align=""center"">"
    strRecordListHtml = strRecordListHtml & strPageInfo          
    strRecordListHtml = strRecordListHtml & "</td>"
    strRecordListHtml = strRecordListHtml & "</tr>"&vbCRLF
 objRecordSet.close
 set objRecordSet = nothing
%>

里面用到了一些函数,细节就自己去想吧!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值