oioj’s blog     powered by oBlog version 2.0 oioj Studio 2004


ASP通用分页代码已经写成类
メ冰枫ぱ雪 发表于 2005-6-23 2:17:24

ASP通用分页类

一、类的属性、方法
  这是一个ASP通用分页类。你可以用它方便的给记录集分页,当然在网上有许多的分页类和分页函数。本分页主要有Conn、SQL、URL、FieldList、PageSize、Template、Init、Show、Close等属性和方法。
  1.Conn属性    :用来获取一个已创建的Connection对象。
  2.SQL属性      :根据传入的SQL语句,自动创建RecordSet对象
  3.PageSize属性    :设置第页的记录条数
  4.FieldList属性    :如果需要显示库中的数据,一定要设置此属性。它是用来获取需要显示的正确的字段名。
  5.URL属性      :设置分页条中链接的文件地址。
  6.Template属性    :设置分页文件的模版。
  7.Init过程     :分页类初始化数据。
  8.Show过程   :显示数据。
  9.Close过程     :分页类结束

二、类的源代码(inc_clsPage.asp)

<%
  '/************************************************
  '*                                               *
  '* Name    : Asp pagnation class                 *
  '* Author  : cjj                                 *
  '* Version : V2.8                                *
  '* Time    : 2004-01-16                          *
  '* Comefrom: http://www.blueidea.com/            *
  '* HomePage: None (Maybe Soon)                   *
  '* Notice  :   You can use and modify it freely, *
  '*           BUT PLEASE HOLD THIS ITEM.          *
  '*             If you modify it that i hope you  *
  '*           can send a mail for me.             *
  '*                                               *
  '************************************************/

  '#########声明变量#########

  '定义链接的显示类型
  Const SW_intShowChinese     = 0  '汉字,如"上一页、下一页"
  Const SW_intShowEnglish     = 1  '英文,如"Back、Next"
  Const SW_intShowCharacter   = 2  '特殊字符
  '定义数据的显示类型
  Const SW_intHorizontalView  = 0  '横排
  Const SW_intVerticalView    = 1  '竖排
  '线的显示类型
  Const SW_intShowBigBorder   = 2  '粗线
  Const SW_intShowSmallBorder = 1  '细线
  Const SW_intShowNoBorder    = 0  '无线

  '标题栏线的显示类型
  Const SW_intTleSmallBorder = 1  '细线
  Const SW_intTleNoBorder    = 0  '无线

  '显示分页信息的位置
  Const SW_intPageInNone      = 0  '不显示分页条
  Const SW_intPageInTop       = 1  '在顶部显示
  Const SW_intPageInBottom    = 2  '在底部显示
  Const SW_intPageInAll       = 3  '在顶、底部显示

  '显示的页数的数字个数
  Const SW_intShowPages       = 10

  'SWPage Class
  Private SW_blnIsHaveRS,SW_strFields,SW_blnInConn,SW_intShowPagePos,SW_strShowWidth
  '#########初始化变量##########
  SW_blnIsHaveRS    = false      '设置记录集获取标志为Fasle,就是无记录集状态
  SW_strShowWidth   = "90%"      '默认的显示宽度为90%
  SW_strFields      = ""
  SW_blnInConn      = true       '由内部创建Connection,如果设为False则由外部创建
  SW_intShowPagePos = SW_intPageInAll


  '*****************************************
  ' 类型:    类
  ' 目的:    给记录集分页
  '*****************************************
  Class SWPage
      '声明类私有变量
      Private SW_objRS,SW_objConn

   Private SW_strSQL,SW_strURL,SW_strError,SW_strFormAction,SW_strHeadJS,SW_strFootJS

   Private SW_intPageSize,SW_intTleBDType,SW_intShowPageType,SW_intShowType
      Private SW_lngTotalPage,SW_lngTotalRecord,SW_lngPageNo
      Private SW_aryFldName,SW_aryFldNote,SW_aryFldWidth,SW_aryFldAlign,SW_aryFldLink
   Private SW_blnInit,SW_blnOpenRS,SW_blnConn,SW_blnPageSize,SW_blnFieldList,SW_blnGetURL,SW_blnShowType,SW_blnShowPageType,SW_blnTemplate

      '*****************************************
   ' 类型:    属性
      ' 目的:    根据获取的信息,创建数据库连接
      ' 输入:    a_strConn:数据类型字符串
      ' 返回:    无
      '*****************************************
      Public Property Let Conn(a_strConn)
          Dim strError
          SW_blnConn = false
          Set SW_objConn = CreateObject("Adodb.Connection")
          On Error Resume Next
          SW_objConn.Open a_strConn
          If Err.Number <> 0 Then strError = strError & "<br>创建Connection对象失败"
          On Error Goto 0   
    
          If Trim(strError)="" Then
              SW_blnConn = true
          Else
              SW_strError = SW_strError & "  <tr bgcolor=""#336699"" height=""28""><td><font color=""#FFFFFF"">Conn属性:</font></td></tr>" & vbcrlf
              SW_strError = SW_strError & "  <tr bgcolor=""#ffffff""><td>" & strError & "</td></tr>" & vbcrlf
          End If
   End Property

      '*****************************************
   ' 类型:    属性
      ' 目的:    设定或显示SQL语句。
      ' 输入:    a_strSQL:   SQL语句。
      ' 返回:    SQL语句。
      '*****************************************
   Public Property Let SQL(a_strSQL)
          Dim strError

    SW_blnOpenRS = false
    SW_strSQL = a_strSQL

          '创建RecordSet对象
       Set SW_objRS = CreateObject("adodb.RecordSet")
          On Error Resume Next
    SW_objRS.Open SW_strSQL,SW_objConn,1,1
    If Err.Number <> 0 Then strError = "<br>记录集打开失败"
          On Error Goto 0

          If Trim(strError)="" Then
              SW_blnOpenRS = true
          Else
              SW_strError = SW_strError & "   <tr bgcolor=""#336699"" height=""28""><td><font color=""#FFFFFF"">SQL属性:</font></td></tr>" & vbcrlf
              SW_strError = SW_strError & "  <tr bgcolor=""#ffffff""><td>" & strError & "</td></tr>" & vbcrlf
          End If
   End Property


      '*****************************************
   ' 类型:    属性
      ' 目的:    设置需要显示的数据的字段名
      ' 输入:    a_strFldName:字段名字符串,多个字段之间以逗号分隔
      ' 返回:    无
      '*****************************************
      Public Property Let FieldList(a_strFldName)
          Dim strError

    SW_blnFieldList = false
    If Trim(a_strFldName) <> "" Then
              SW_aryFldName = Split(LCase(a_strFldName),",")
          Else
              strError = strError & "<br>你必须设置需要显示的字段名,否则无法显示数据"
    End If
          If Trim(strError)="" Then
              SW_blnFieldList = true
          Else
              SW_strError = SW_strError & "  <tr bgcolor=""#336699"" height=""28""><td><font color=""#FFFFFF"">FieldList属性:</font></td></tr>" & vbcrlf
              SW_strError = SW_strError & "  <tr bgcolor=""#ffffff""><td>" & strError & "</td></tr>" & vbcrlf
          End If
   End Property

      '*****************************************
   ' 类型:    属性
      ' 目的:    设定或显示每页的记录数。
      ' 输入:    a_intPageSize:   每页显示的记录数。
      ' 返回:    每页显示的记录数。
      '*****************************************
   Public Property Let PageSize(a_intPageSize)
          Dim strError

    'PageSize获取失败标志
    SW_blnPageSize = false
    If Trim(a_intPageSize) = "" OR (Not(IsNumeric(a_intPageSize))) Then
              strError = strError & "<br>非法的pagesize"
    Else
              If (a_intPageSize <= 2147483647 And a_intPageSize>=-2147483648) Then
                  SW_intPageSize = CLng(a_intPageSize)
              Else
                  strError = strError & "<br>PageSize溢出"
     End If
     If (SW_intPageSize<=0) Then
                  strError = strError & "<br>PageSize只能是正整数"
              End If
    End If
          If Trim(strError)="" Then
              SW_blnPageSize = True
          Else
              SW_strError = SW_strError & "  <tr bgcolor=""#336699"" height=""28""><td><font color=""#FFFFFF"">PageSize属性:</font></td></tr>" & vbcrlf
              SW_strError = SW_strError & "  <tr bgcolor=""#ffffff""><td>" & strError & "</td></tr>" & vbcrlf
          End If
   End Property

      '*****************************************
   ' 类型:    属性
      ' 目的:    设定分页文件显示模板信息。
      ' 输入:    a_strTemplate:   模板描述。
      ' 返回:    无。
      '*****************************************
   Public Property Let Template(a_strTemplate)
          Dim aryTemplate,i,j,strError,strTemp,intItem,aryTemp,aryTemp1

          SW_blnTemplate = false

          aryTemplate = Split(Trim(LCase(a_strTemplate)),"+")
          intItem = UBound(aryTemplate)
          If intItem < 4 Then strError = "<br>此属性是必选项"

          If intItem < 4 Then
              '获取文件头
              strTemp = TriM(aryTemplate(0))

              If strcomp(Left(strTemp,6),"[file=")= 0 Then
                  SW_strHeadJS = "<script src=""" & Mid(strTemp,7,Len(strTemp)-7) & """></script>"
              Else
                  SW_strHeadJS = Mid(strTemp,7,Len(strTemp)-7)
              End If

              '获取文件尾
              strTemp = Trim(aryTemplate(3))

              If strComp(Left(strTemp,6),"[file=") = 0 Then
                  SW_strFootJS = "<script src=""" & Mid(strTemp,7,Len(strTemp)-7) & """></script>"
              Else
                  SW_strFootJS = Mid(strTemp,7,Len(strTemp)-7)
              End If

              '获取分页模版
     aryTemp = Split(aryTemplate(1),",")
              If aryTemp(0) <> "" Then strTemp = Replace(aryTemp(0),"[page=","")

              If IsNumeric(strTemp) Then strTemp = CInt(strTemp)
              If strTemp <> SW_intPageInNone Then

                  If IsNumeric(strTemp) Then strTemp = CInt(strTemp)

                  SW_intShowPagePos = strTemp
                  strTemp = aryTemp(1)
                  If strTemp <> "" Then strTemp = Replace(strTemp,"]","")

                  If IsNumeric(strTemp) Then strTemp = CInt(strTemp)
                  SW_intShowPageType = strTemp
              Else
                  SW_intShowPagePos = SW_intPageShowInNone
              End If

              '获取数据显示模版
     aryTemp = Split(aryTemplate(2),"|")
              For i = 0 To UBound(aryTemp)
                   strTemp = LCase(Trim(aryTemp(i)))
                   Select Case i
                   Case 0  '获取并设置显示宽度
                       If strTemp <> "" Then strTemp = Replace(strTemp,"[data=","")
                       SW_strShowWidth = strTemp
                   Case 1  '获取数据显示方式
                       If IsNumeric(strTemp) Then strTemp = CInt(strTemp)
                       SW_intShowType = strTemp
                   Case 2
                       If strTemp <> "" Then strTemp = Replace(strTemp,"]","")
                       aryTemp1 = Split(Trim(strTemp),"#")
                       If UBound(aryTemp1) >= 3 Then
                           SW_aryFldNote = Split(aryTemp1(0),",")
                           SW_aryFldAlign = Split(aryTemp1(1),",")
                           SW_aryFldWidth = Split(aryTemp1(2),",")
                           SW_aryFldLink = Split(aryTemp1(3),",")
                       End If
                   End Select
              Next
          End If

          If strError = "" Then
              SW_blnTemplate = true
          Else
              SW_strError = SW_strError & "  <tr bgcolor=""#336699"" height=""28""><td><font color=""#FFFFFF"">Template属性:</font></td></tr>" & vbcrlf
              SW_strError = SW_strError & "  <tr bgcolor=""#ffffff""><td>" & strError & "</td></tr>" & vbcrlf
          End If
      End Property

      '*****************************************
   ' 类型:    属性
      ' 目的:    设定或显示URL。
      ' 输入:    a_strURL:   需要分页的文件地址。
      ' 返回:    需要分页的文件地址。
      '*****************************************
   Public Property Let url(/a_strURL)
          Dim strError,objFSO

    SW_blnGetURL = false
    If Trim(a_strURL)="" Then
              strError = "<br>非法的URL地址"
          Else
     Set objFSO = CreateObject("Scripting.FileSystemObject")
     If objFSO.FileExists(Server.Mappath(a_strURL)) Then
         SW_strURL = a_strURL
              Else
                  strError = strError & "<br>你能确定文件 <font color=""#ff0000"">" & a_strURL & "</font> 存在么?"
     End If
     Set objFSO = Nothing
    End If
          If strError = "" Then
              SW_blnGetURL = true
          Else
              SW_strError = SW_strError & "  <tr bgcolor=""#336699"" height=""28""><td><font color=""#FFFFFF"">URL属性:</font></td></tr>" & vbcrlf
              SW_strError = SW_strError & "  <tr bgcolor=""#ffffff""><td>" & strError & "</td></tr>" & vbcrlf
          End If
   End Property


   '*****************************************
   ' 类型:    属性
      ' 目的:    设置显示标题时单元格的宽度
      ' 输入:    a_strTleWidth:单元格宽度字符串
      ' 返回:    无
      '*****************************************
      Public Property Let TleWidth(a_strWidth)
          SW_strTleWidth = a_strWidth
      End Property

   '*****************************************
   ' 类型:    属性
      ' 目的:    设置显示标题时单元格分隔线的类型
      ' 输入:    a_intBDType 单元格分隔线的类型
      ' 返回:    无
      '*****************************************
      Public Property Let TleBDType(a_intBDType)
          SW_intTleBDType = a_intBDType
      End Property

   '*****************************************
   ' 类型:    过程
      ' 目的:    统计总记录数、计算总页数
      ' 输入:    无
      ' 返回:    无
      '*****************************************
   Sub Init()
          Dim intPostion,strError,i,objRSTemp,lngTotalRecord

          If Not (SW_blnConn and SW_blnOpenRS and SW_blnPageSize and SW_blnGetURL) Then
    Call ShowErrors()
    Exit Sub
          End If

    If SW_objRS.Eof And SW_objRS.Bof Then
             strError = strError & "<br>库中无任何记录"
          End If

    '计算总计录数
          Set objRSTemp = SW_objConn.Execute("SELECT * FROM SW_RecCount")
          lngTotalRecord = CLng(objRSTemp("SW_RecCount").Value)
     SW_lngTotalRecord = lngTotalRecord
         If (SW_lngTotalRecord<=2147483647 AND SW_lngTotalRecord>=-2147483648) Then
              SW_lngTotalRecord = CLng(SW_lngTotalRecord)
          Else
              strError = strError & "<br>分页初始化时:总记录数溢出"
    End If
          If SW_lngTotalRecord <=0 Then strError = strError & "<br>分页初始化时:总记录数小于零"

    '计算总页数
          If SW_lngTotalRecord Mod SW_intPageSize = 0 Then
        SW_lngTotalPage = CLng(SW_lngTotalRecord \ SW_intPageSize * -1)*-1
          Else
     SW_lngTotalPage = CLng(SW_lngTotalRecord \ SW_intPageSize * -1)*-1 + 1
          End If

    '获取页数
    SW_lngPageNo = Trim(Request.QueryString("pageno"))
    If SW_lngPageNo = "" Then SW_lngPageNo = Trim(Request.Form("PageNo"))
    If SW_lngPageNo = "" Then SW_lngPageNo = 1
          '如果没有选择第几页,则默认显示第一页
          If SW_lngPageNo <> "" And IsNumeric(SW_lngPageNo) Then
              If (SW_lngPageNo <= 2147483647 And SW_lngPageNo>=-2147483648) Then
                  SW_lngPageNo = CLng(SW_lngPageNo)
              Else
                  strError = strError & "<br>页数溢出,请检查!"
       End If
     If (SW_lngPageNo<=0) Then strError = strError & "<br>页数只能是正整数!"
          Else
              strError = strError & "<br>你确信此页数 <font color=""#FF0000"">" & SW_lngPageNo & "</font> 存在?"
       End If

    If (SW_lngPageNo > SW_lngTotalPage AND SW_lngTotalPage<>0) Then SW_lngPageNo = SW_lngTotalPage

          SW_objRS.PageSize     = SW_intPageSize
       SW_objRS.AbsolutePage = SW_lngPageNo

    intPostion = InstrRev(SW_strURL,"?")
       SW_strFormAction = SW_strURL
       If intPostion > 0 Then
           SW_strURL = SW_strURL & "&PageNo="
          Else
           SW_strURL = SW_strURL & "?PageNo="
       End If

          If Trim(SW_strFields) = "" Then
              For i = 0 To SW_objRS.Fields.Count-1
                  SW_strFields = SW_strFields & SW_objRS(i).Name & ","
     Next
          End If

          SW_strFields = LCase("," & Trim(SW_strFields))
    If IsArray(SW_aryFldName) Then
           For i = LBound(SW_aryFldName) To Ubound(SW_aryFldName)
      If  Instr(SW_strFields,"," & SW_aryFldName(i) & ",") = 0 Then strError = strError & "<br>字段名 <font color=""#ff0000"">" & SW_aryFldName(i) & "</font> 正确吗?"
     Next

     If (IsArray(SW_aryFldNote) AND UBound(SW_aryFldName)>UBound(SW_aryFldNote)) Then strError = strError & "<br>字段相应中文说明项目不能小于字段个数"

        If (IsArray(SW_aryFldWidth) AND UBound(SW_aryFldName)>UBound(SW_aryFldWidth)) Then strError = strError & "<br>字段相应宽度项目不能小于字段个数"
  
        If (IsArray(SW_aryFldAlign) AND UBound(SW_aryFldName)>UBound(SW_aryFldAlign)) Then strError = strError & "<br>字段数据相应对齐方式项目不能小于字段个数"
        If (IsArray(SW_aryFldLink) AND UBound(SW_aryFldName)>UBound(SW_aryFldLink)) Then strError = strError & "<br>字段数据相应文件链接项目不能小于字段个数"
    End If

    If Trim(strError) = "" Then
              SW_blnInit = true
          Else
              SW_strError = SW_strError & "  <tr bgcolor=""#336699"" height=""28""><td><font color=""#FFFFFF"">Init过程:</font></td></tr>" & vbcrlf
              SW_strError = SW_strError & "  <tr bgcolor=""#ffffff""><td>" & strError & "</td></tr>" & vbcrlf
          End If
          Response.Write(SW_strHeadJS & vbcrlf)
  End Sub
      
      '*****************************************
   ' 类型:    过程
      ' 目的:    显示分页信息
      ' 输入:    无
      ' 返回:    无
      '*****************************************
      Private Sub Pages()
          Dim strPages,k,intTemp,intTemp1

    If Not(SW_blnInit) Then  Call ShowErrors()
 
    If SW_lngTotalPage = 1 Then Exit Sub
          Response.Write("<table align=""center"" class=""css_ShowPage"" width=""" & SW_strShowWidth & """>" & vbcrlf)

          Response.Write("  <tr>" & vbcrlf)
          Response.Write("    <td>" & vbcrlf)
          Response.Write("      <table width=""100%"">" & vbcrlf)
          Response.Write("         <tr>" & vbcrlf & "           <td align=""center"" valign=""middle"" class="".test1"">" & vbcrlf)

    If SW_lngTotalPage >= 1 Then
              SELECT CASE SW_intShowPageType
              Case SW_intShowChinese
                  If SW_lngPageNo <= 1 Then
                      Response.Write ("<font color=""" & SW_strFTColor & """>首页 前页 <a href=""" & SW_strURL & SW_lngPageNo+1 & """>后页</a> <a href=""" & SW_strURL & SW_lngTotalPage & """>末页</a>" & vbcrlf)
               Else
                If SW_lngPageNo >= SW_lngTotalPage Then
                          Response.Write ("<font color=""" & SW_strFTColor & """><a href=""" & SW_strURL & "1"">首页</a>  <a href=""" & SW_strURL  & SW_lngPageNo -1 & """>前页</a>  " & "后页  末页" & vbcrlf)
                      Else
                          Response.Write ("<font color=""" & SW_strFTColor & """><a href=""" & SW_strURL & "1"">首页</a> <a href=""" & SW_strURL  & SW_lngPageNo -1 & """>前页</a> " & "<a href=""" & SW_strURL  & SW_lngPageNo+1 & """>后页</a> <a href=""" & SW_strURL  & SW_lngTotalPage & """>末页</a>" & vbcrlf)
                   End If
               End If
               Response.Write (" 页次:<b>" & SW_lngPageNo & "</b>/" & SW_lngTotalPage & "页 共<b>" & SW_lngTotalRecord & "</b>条记录 <b>" & SW_intPageSize & "</b>条/页</td>" & vbcrlf)
                  Response.Write("      <form name=""gopage"" action=""" & SW_strFormAction & """ method=""post"">" & vbcrlf)
                  Response.Write("      <td> 第")
         Response.Write("    <input type=""text"" name=""pageno"" size=""3"" maxlength=4 title=""请输入页号,然后回车"">页 " & vbcrlf)
                  Response.Write("<input type=""submit"" value="/"GO""></td></form></tr>" & vbcrlf)
              Case SW_intShowEnglish
                  If SW_lngPageNo <= 1 Then
                      Response.Write ("<font color=""" & SW_strFTColor & """>First Prev <a href=""" & SW_strURL & SW_lngPageNo+1 & """>Next</a> <a href=""" & SW_strURL & SW_lngTotalPage & """>Last</a>" & vbcrlf)
               Else
                If SW_lngPageNo >= SW_lngTotalPage Then
                          Response.Write ("<font color=""" & SW_strFTColor & """><a href=""" & SW_strURL & "1"">First</a>  <a href=""" & SW_strURL  & SW_lngPageNo -1 & """>Prev</a>  " & "Next  Last" & vbcrlf)
                      Else
                          Response.Write ("<font color=""" & SW_strFTColor & """><a href=""" & SW_strURL & "1"">First</a> <a href=""" & SW_strURL  & SW_lngPageNo -1 & """>Prev</a> " & "<a href=""" & SW_strURL  & SW_lngPageNo+1 & """>Next</a> <a href=""" & SW_strURL  & SW_lngTotalPage & """>Last</a>" & vbcrlf)
                   End If
               End If
               Response.Write (" Page No:<b>" & SW_lngPageNo & "</b>/" & SW_lngTotalPage & " Total Records:<b>" & SW_lngTotalRecord & "</b> PageSize:<b>" & SW_intPageSize & "</b></td>" & vbcrlf)
                  Response.Write("      <form name=""gopage"" action=""" & SW_strFormAction & """ method=""post"">" & vbcrlf)
                  Response.Write("      <td> ")
         Response.Write("    <input type=""text"" name=""pageno"" size=""3""  maxlength=4 title=""Please input pageno then enter""> " & vbcrlf)
                  Response.Write("<input type=""submit"" value=""GO""></td></form></tr>" & vbcrlf)
          &nbs

阅读全文() | 回复(0) | 引用通告() | 编辑

发表评论:

    昵称:
    密码:
    主页:
    标题:

 

  公告

  我的分类(专题)
  最近日志

  最新评论

  留言板

  链接

  Blog信息



Powered by Oblog.