一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

asp 通用分页类

时间:2022-07-02 23:10:11 编辑:袖梨 来源:一聚教程网


<%@LANGUAGE = "VBScript" CODEPAGE="936"%>
<%Option Explicit%>


Class Cls_PageView
 Private sbooInitState
 Private sstrPageUrl
 Private sstrPageVar
 Private sstrSql
 Private sstrSqlCount

 Private sintRecordCount
 Private sintPageSize
 Private sintPageNow
 Private sintPageMax

 Private sobjConn

 Private sstrPageInfo

 Private Sub Class_Initialize
  Call ClearVars()
 End Sub

 Private Sub class_terminate()
  Set sobjConn = nothing
 End Sub

 Public Sub ClearVars()
  sbooInitState = False
  sstrPageUrl = ""
  sstrPageVar = "page"

  sintRecordCount = 0
  sintPageSize = 20
  sintPageNow = 0
  sintPageMax = 0
 End Sub

 Private Sub ClearMainVars()
  sstrSql = ""
 End Sub

 Rem ## SQL语句
 Public Property Let strSQL(Value)
  sstrSql = Value
 End Property

 Rem ## SQL语句
 Public Property Let strSQLCount(Value)
  sstrSqlCount = Value
 End Property

 Rem ## 转向地址
 Public Property Let strPageUrl(Value)
  sstrPageUrl = Value
 End Property

 Rem ## 每页显示的记录条数
 Public Property Let intPageSize(Value)
  sintPageSize = toNum(Value, 20)
 End Property

 Rem ## 数据库连接对象
 Public Property Let objConn(Value)
  Set sobjConn = Value
 End Property

 Rem ## 当前页
 Public Property Let intPageNow(Value)
  sintPageNow = toNum(Value, 1)
 End Property

 Rem ## 设置记录总数
 Public Property Let intRecordCount(Value)
  sintRecordCount = toNum(Value, -1)
  If sintRecordCount < 0 Then sintRecordCount = -1
 End Property

 Rem ## 页面参数
 Public Property Let strPageVar(Value)
  sstrPageVar = Value
 End Property

 Rem ## 获得当前页
 Public Property Get intPageNow()
  intPageNow = singPageNow
 End Property

 Rem ## 分页信息
 Public Property Get strPageInfo()
  strPageInfo = sstrPageInfo
 End Property

 Rem ## 取得记录集, 二维数组或字串, 在进行循环输出时必须用 IsArray() 判断
 Public Property Get arrRecordInfo()
  Call InitClass()
  If Not sbooInitState Then
   Response.Write("分页类初始化失败, 请检查各参数情况")
   Exit Property
  End If

  Dim rs, sql
  sql = sstrSql

  Set rs = Server.CreateObject("Adodb.RecordSet")

  Rem 若记录数统计语句不为空, 则取语句执行后第一个字段值作为记录数
  If sstrSqlCount <> "" Then
   rs.Open sstrSqlCount, sobjConn, 1, 1
   If Not(rs.eof or rs.bof) Then
    sintRecordCount = rs(0)
   Else
    sintRecordCount = 0
   End If
   rs.Close
  End If

  rs.open sql, sobjConn, 1, 1

  Rem 若无记录统计语句且未设定记录总数, 则由记录集RecordCount属性得出.
  If sintRecordCount < 0 Then
   sintRecordCount = rs.RecordCount
  End If
  If sintRecordCount < 0 Then sintRecordCount = 0

  '生成分页信息
  Call InitPageInfo()

  If Not(rs.eof or rs.bof) Then
   rs.PageSize = sintPageSize
   rs.AbsolutePage = sintPageNow
   If Not(rs.eof or rs.bof) Then
    arrRecordInfo = rs.getrows(sintPageSize)
   Else
    arrRecordInfo = ""
   End If
  Else
   arrRecordInfo = ""
  End If
  rs.close
  Set rs = nothing

  Call ClearMainVars()
 End Property

 Rem ## 初始化分页信息
 Private Sub InitPageInfo()
  sstrPageInfo = ""

  Dim surl
  surl = sstrPageUrl
  If Instr(1, surl, "?", 1) > 0 Then
   surl = surl & "&" & sstrPageVar & "="
  Else
   surl = surl & "?" & sstrPageVar & "="
  End If

  If sintPageNow <= 0 Then sintPageNow = 1
  If sintRecordCount mod sintPageSize = 0 Then
   sintPageMax = sintRecordCount sintPageSize
  Else
   sintPageMax = sintRecordCount sintPageSize + 1
  End If
  If sintPageNow > sintPageMax Then sintPageNow = sintPageMax

  If sintPageNow <= 1 then
   sstrPageInfo = "首页 上一页"
  Else
   sstrPageInfo = sstrPageInfo & " 首页"
   sstrPageInfo = sstrPageInfo & " 上一页"
  End If

  If sintPageMax - sintPageNow < 1 then
   sstrPageInfo = sstrPageInfo & " 下一页 末页 "
  Else
   sstrPageInfo = sstrPageInfo & " 下一页 "
   sstrPageInfo = sstrPageInfo & " 末页 "
  End If

  sstrPageInfo = sstrPageInfo & " 页次:" & sintPageNow & " / " & sintPageMax & " "
  sstrPageInfo = sstrPageInfo & " 共 " & sintRecordCount & " 条记录 " & sintPageSize & " 条/页 "
 End Sub

 Rem ## 长整数转换
 Private function toNum(s, Default)
  s = s & ""
  If s <> "" And IsNumeric(s) Then
   toNum = CLng(s)
  Else
   toNum = Default
  End If
 End function

 Rem ## 类初始化
 Public Sub InitClass()
  sbooInitState = True
  If Not(IsObject(sobjConn)) Then
   sbooInitState = False

   response.write("数据库连接未指定")
   response.End()
  End If
  If Trim(sstrSql) = "" Then
   sbooInitState = False

   response.write("SQL语句未指定")
   response.End()
  End If
  sintPageSize = toNum(sintPageSize, 20)
  If (sintPageSize < 1) Or (sintPageSize > 100) Then
   sbooInitState = False

   response.write("每页记集数未设置或不符合规则(1 - 100)")
   response.End()
  End If
  sintPageNow = toNum(sintPageNow, 1)

  sintRecordCount = -1
 End Sub
End Class
<%
 response.Buffer = True
 Dim intDateStart
 intDateStart = Timer()

 Rem ## 打开数据库连接
 Rem #################################################################
  function f__OpenConn()
   Dim strDbPath
   Dim connstr
   strDbPath = "./db.mdb"
   connstr  = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
   connstr  = connstr & Server.MapPath(strDbPath)
   Set conn  = Server.CreateObject("Adodb.Connection")
   conn.open connstr
  End function
 Rem #################################################################
 
 Rem ## 关闭数据库连接
 Rem #################################################################
  function f__CloseConn()
   If IsObject(conn) Then
    conn.close
   End If
   Set conn = nothing
  End function
 Rem #################################################################

 Rem 获得执行时间
 Rem #################################################################
 function getTimeOver(iflag)
  Dim tTimeOver
  If iflag = 1 Then
   tTimeOver = FormatNumber(Timer() - intDateStart, 6, true)
   getTimeOver = " 本页执行时间: " & tTimeOver & " 秒"
  Else
   tTimeOver = FormatNumber((Timer() - intDateStart) * 1000, 3, true)
   getTimeOver = " 本页执行时间: " & tTimeOver & " 毫秒"
  End If
 End function
 Rem #################################################################

 Dim strLocalUrl
 strLocalUrl = request.ServerVariables("SCRIPT_NAME")
 
 Dim intPageNow
 intPageNow = request.QueryString("page")
 
 Dim intPageSize, strPageInfo
 intPageSize = 30
 
 Dim arrRecordInfo, i
 Dim Conn, sql, sqlCount
 sql = "SELECT [ID], [aaaa], [bbbb], [cccc]" & _
  " FROM [table1]" & _
  " ORDER BY [ID] DESC"
 sqlCount = "SELECT Count([ID])" & _
   " FROM [table1]"
 f__OpenConn
  Dim clsRecordInfo
  Set clsRecordInfo = New Cls_PageView
  
  Rem 记录集总数取值优先顺序: strSqlCount >>  intRecordCount
  Rem 即当 strSqlCount 有值时, intRecordCount 无作用
  Rem 因此, 若要手工设置记录总数, 请设置 intRecordCount, strSqlCount 留空
  Rem 若以上两者都没有设置, 则取 strSql 执行后的 RecordCount 属性.
   clsRecordInfo.intRecordCount = 2816
   clsRecordInfo.strSqlCount = sqlCount
  Rem 此处因设置了 strSqlCount, 则记录总数将由此语句计算得出.
  
  Rem 设置 SQL 查询语句
   clsRecordInfo.strSql = sql
  
  Rem 设置每页显示数
   clsRecordInfo.intPageSize = intPageSize
  
  Rem 设置当前显示页
   clsRecordInfo.intPageNow = intPageNow
  
  Rem 设置转向页面
   clsRecordInfo.strPageUrl = strLocalUrl
  
  Rem 设置页面转向参数
   clsRecordInfo.strPageVar = "page"

  clsRecordInfo.objConn = Conn  
  arrRecordInfo = clsRecordInfo.arrRecordInfo
  strPageInfo = clsRecordInfo.strPageInfo
  Set clsRecordInfo = nothing
 f__CloseConn
%>



萧月痕通用分页类1.2 测试
css" type="text/css">




 
   
   
   
   
 
 <%
  Dim bgColor
  If IsArray(arrRecordInfo) Then
   For i = 0 to UBound(arrRecordInfo, 2)
   bgColor="#FFFFFF"
   if i mod 2=0 then bgColor="#DFEFFF"
 %>
 
   
   
   
   
 
 <%
   Next
  End If
 %>
ID标题内容(显示前20个字)时间
<%= arrRecordInfo(0, i)%><%= arrRecordInfo(1, i)%><%= arrRecordInfo(2, i)%><%= arrRecordInfo(3, i)%>



 
  
 
 
  
 
<%= strPageInfo%>
<%= getTimeOver(0)%>


热门栏目