<%@LANGUAGE = "VBScript" CODEPAGE="936"%>
<%Option Explicit%>
<!--#include file="Cls_PageView.asp"-->
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 & " <a href=""" & surl & "1"">首页</a>"
sstrPageInfo = sstrPageInfo & " <a href=""" & surl & (sintPageNow - 1) & """>上一页</a>"
End If
If sintPageMax - sintPageNow < 1 then
sstrPageInfo = sstrPageInfo & " 下一页 末页 "
Else
sstrPageInfo = sstrPageInfo & " <a href=""" & surl & (sintPageNow + 1) & """>下一页</a> "
sstrPageInfo = sstrPageInfo & " <a href=""" & surl & sintPageMax & """>末页</a> "
End If
sstrPageInfo = sstrPageInfo & " 页次:<strong><font color=""#990000"">" & sintPageNow & "</font> / " & sintPageMax & " </strong>"
sstrPageInfo = sstrPageInfo & " 共 <strong>" & sintRecordCount & "</strong> 条记录 <strong>" & sintPageSize & "</strong> 条/页 "
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
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>萧月痕通用分页类1.2 测试</title>
<link rel="stylesheet" href="page.css" type="text/css">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="PageView">
<table width="760" border="1" cellspacing="0" cellpadding="4" align="center" bordercolordark="#FFFFFF" bordercolorlight="#CCCCCC">
<tr align="center">
<td width="60">ID</td>
<td width="150">标题</td>
<td width="*">内容(显示前20个字)</td>
<td width="150">时间</td>
</tr>
<%
Dim bgColor
If IsArray(arrRecordInfo) Then
For i = 0 to UBound(arrRecordInfo, 2)
bgColor="#FFFFFF"
if i mod 2=0 then bgColor="#DFEFFF"
%>
<tr bgcolor="<%=bgColor%>">
<td width="60"><%= arrRecordInfo(0, i)%></td>
<td width="150"><%= arrRecordInfo(1, i)%></td>
<td width="*"><%= arrRecordInfo(2, i)%></td>
<td width="150"><%= arrRecordInfo(3, i)%></td>
</tr>
<%
Next
End If
%>
</table>
</div>
<table width="760" border="0" cellspacing="0" cellpadding="4">
<tr>
<td><%= strPageInfo%></td>
</tr>
<tr>
<td align="center"><%= getTimeOver(0)%></td>
</tr>
</table>
</body>
</html>