海量信息ASP分页类增强版

<%
'By:八星瓢虫
'Ver:2.0
'特点:速度快,调用自由,如:rs("字段1")或rs(1)
Const L_First="9"
Const L_Prev="3"
Const L_Next="4"
Const L_Last=":"

Class TheokPage
Private ThisConn
Private ThisRs
Private ThisTable
Private ThisWhere
Private ThisOrder
Private ThisID
Private ThisPageSize '每页显示数
Private RsNum '总记录数
Private PageNum '总页数
Private PageUrl '动态URL
Private ThisPage '当前页面值
Private IsRs '数据是否已经初始化
Private ShowPage_ '分页导航缓存
Private ErrInfo '错误提示
'类初始化
Private Sub Class_Initialize
   ThisPageSize = 10 '默认每页显示个数
   ThisWhere = ""
   ThisOrder = ""
   ThisID = ""
   ShowPage_ = ""
   IsRs = False
   If Request("page")="" Then
    ThisPage=1
   ElseIf not(IsNumeric(Request("page"))) Then
    ThisPage=1
   ElseIf CLng(Trim(Request("page")))<1 Then
    ThisPage=1
   Else
    ThisPage=CLng(Trim(Request("page")))
   End If
End Sub
'得到数据库连接
Public Property Let pConn(oconn)
   Set ThisConn = oconn
   If IsObject(ThisConn) = False Then
    ErrInfo=ErrInfo & "数据库连接错误!"
    ShowError()
   End If
End Property
'得到表名
Public Property Let pTable(svalue)
   If svalue <> "" Then
    ThisTable = svalue
   Else
    ErrInfo=ErrInfo & "表名不能为空"
    ShowError()
   End If
End Property
'得到条件
Public Property Let pWhere(svalue)
   If svalue<>"" Then
    ThisWhere = " Where " & svalue
   End If
End Property
'得到排序
Public Property Let pOrder(svalue)
   If svalue<>"" Then
    ThisOrder = " orDER BY " & svalue
   End If
End Property
'获取主键
Public Property Let pID(svalue)
   If svalue <> "" Then
    ThisID = svalue
   Else
    ErrInfo=ErrInfo & "主键不能为空"
    ShowError()
   End If
End Property
'获取每页记录数
Public Property Let PageSize(ivalue)
   If IsNumeric(ivalue) Then
    ThisPageSize=CLng(ivalue)
    If ThisPageSize < 1 Then ThisPageSize = 10
   Else
    ErrInfo=ErrInfo & "PageSize的参数不正确"
    ShowError()
   End If
End Property
'返回每页个数
Public Property Get PageSize
   If ThisPageSize="" or (not(IsNumeric(ThisPageSize))) Then
    PageSize=10
   Else
    PageSize=ThisPageSize
   End If
End Property
'获取记录集
Public Property Get GetRs()
   Dim RsNumSql,RsNum_
   RsNumSql = "select count("&ThisID&") from "& ThisTable & ThisWhere
   set RsNum_ = ThisConn.Execute(RsNumSql)
   RsNum = RsNum_(0)
   RsNum_.Close
   Set RsNum_ = Nothing
   If RsNum mod ThisPageSize = 0 Then
    PageNum = RsNum \ ThisPageSize
   Else
    PageNum = RsNum \ ThisPageSize + 1
   End If
   If ThisPage > PageNum Then ThisPage = PageNum
   Dim QH,DX
   QH=">"
   DX="MAX"
   If InStr(ThisOrder," desc") Then
    QH="<"
    DX="MIN"
   End If
   Sql = "Select TOP "& ThisPageSize &" * FROM "& ThisTable & ThisWhere
   If ThisPage > 1 Then
    If ThisWhere="" Then
     Sql = Sql &" Where("&ThisID&""&QH&"(Select "&DX&"("&ThisID&") FROM(Select TOP "&ThisPageSize*(ThisPage-1)&" "&ThisID&" FROM "&ThisTable&ThisWhere&ThisOrder&") AS T))"
    Else
     Sql = Sql &" And ("&ThisID&""&QH&"(Select "&DX&"("&ThisID&") FROM(Select TOP "&ThisPageSize*(ThisPage-1)&" "&ThisID&" FROM "&ThisTable&ThisWhere&ThisOrder&") AS T))"
    End If
   End If
   Sql = Sql & ThisOrder
   Set ThisRs = Server.CreateObject("Adodb.RecordSet")
   ThisRs.Open Sql,ThisConn,1,1
   IsRs = true
   Set GetRs = ThisRs
End Property
'显示分页
Public Function ShowPage()
   Dim result,i,m
   PageUrl = GetUrl()
   If Not IsRs Then
    ErrInfo=ErrInfo & "数据执行错误!"
    ShowError()
   End If
   If ShowPage_ <> "" Then
    ShowPage = ShowPage_
    Exit Function
   End If
   result = ""
   If PageNum > 0 Then
    If PageNum = 1 Then
    Else
     '第一页按钮
     If ThisPage = 1 Then
      result = result & L_First & vbCrLf
     Else
      result = result & ""&L_First&"" & vbCrLf
     End If
     '上一数字列按钮
     If ThisPage - 9 > 0 Then
      result = result & ""&L_Prev&"" & vbCrLf
     Else
      If ThisPage = 1 Then
       result = result & L_Prev & vbCrLf
      Else
       result = result & ""&L_Prev&"" & vbCrLf
      End If
     End If
     '数字列按钮
     If ThisPage > 5 and ThisPage + 5 < PageNum Then
      For i = 1 to 9
       m = ThisPage - 5 + i
       If m > 0 and m <= PageNum Then
        If m = ThisPage Then
         result = result & " "&m&" "&vbCrLf
        Else
         result = result & " "&m&" "&vbCrLf
        End If
       End If
      Next
     ElseIf ThisPage <= 5 and ThisPage + 5 < PageNum Then
      For i = 1 to 9
       If i <= PageNum Then
        If i = ThisPage Then
         result = result & " "&i&" "&vbCrLf
        Else
         result = result & " "&i&" "&vbCrLf
        End If
       End If
      Next
     ElseIf ThisPage + 5 >= PageNum Then
      For i = 1 to 9
       If PageNum-9+i > 0 Then
        If PageNum-9+i = ThisPage Then
         result = result & " "&PageNum-9+i&" "&vbCrLf
        Else
         result = result & " "&PageNum-9+i&" "&vbCrLf
        End If
       End If
      Next
     End If
     '下一数字列按钮
     If ThisPage + 9 < PageNum Then
      result = result & ""&L_Next&"" & vbCrLf
     Else
      If ThisPage = PageNum or ThisPage + 5 > PageNum Then
       result = result & L_Next & vbCrLf
      Else
       result = result & ""&L_Next&"" & vbCrLf
      End If
     End If
     '最后一页按钮
     If ThisPage = PageNum or ThisPage + 5 > PageNum Then
      result = result & L_Last & vbCrLf
     Else
      result = result & ""&L_Last&"" & vbCrLf
     End If
     result = result & " "&ThisPage&"/"&PageNum&" 总共:"&RsNum&" "&ThisPageSize&"/页"&vbCrLf
    End If
   Else
    result = "没有符合条件的数据!"
   End If
   ShowPage_ = result
   ShowPage = result
End Function
'获取URL地址
Private function GetURL()
   Dim strUrl,tmp_URL,i,j,search_str,result_url,str_params,str_lparams,str_rparams
   search_str="page="
   strUrl=Request.Servervariables("URL")
   strUrl=split(strUrl,"/")
   i=UBound(strUrl,1)
   tmp_URL=strUrl(i)'得到当前页文件名
   str_params=replace(Trim(Request.Servervariables("QUERY_STRING")),"&_=","")
   If str_params="" Then
    result_url=tmp_URL & "?page="
   Else
    If InstrRev(str_params,search_str)=0 Then
     result_url=tmp_URL & "?" & str_params &"&page="
    Else
     j=InstrRev(str_params,search_str)-2
     If j=-1 Then
      result_url=tmp_URL & "?page="
     Else
      str_lparams=Left(str_params,j)
      str_rparams=right(str_params,len(str_params)-j-1)
      if InStr(str_rparams,"&")<>0 then
       str_rparams=right(str_rparams,len(str_rparams)-InStr(str_rparams,"&")+1)
      else
       str_rparams = ""
      end if
      result_url=tmp_URL & "?" & str_lparams&str_rparams&"&page="
     End If
    End If
   End If
   GetURL=result_url
End function
'销毁类
Private Sub Class_Terminate
   ThisRs.Close
   Set ThisRs=Nothing
End Sub
'错误提示
Private Sub ShowError()
   If ErrInfo <> "" Then
    Response.Write("" & ErrInfo & "")
    Response.End()
   End If
End Sub
End Class
%>

调用示例:
<%
Dim FY
Set FY = New TheokPage
FY.PageSize = 20 '每页显示条数
FY.pConn = Conn '数据库链接对象
FY.pTable = "mytable" '表名
FY.pWhere = "username<>'admin'" '条件,不用加where
FY.pOrder = "id desc" '排序,不用加order by
FY.pID = "id"'主键字段
set Rs = FY.getrs()
%>

<%Do While Not rs.EOF%>

    
    
    
    
    

<%
rs.MoveNext
Loop
Set rs=Nothing
%>
<%=rs("id")%><%=rs("mytitle")%><%=rs("myauthor")%><%=rs("mytime")%><%=rs("myip")%>

<%=FY.ShowPage%>

<%Set FY = Nothing%>




转载:http://hi.baidu.com/cive/blog/item/5cf6c2163e8a3a10962b4322.html


上一篇: MD5加密,16位加密,32位加密(asp版本)
下一篇: Access2000資料庫80萬記錄通用快速ASP分頁類
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 2788
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 300 字 | UBB代码 开启 | [img]标签 关闭