再来一个分页类

转载 2006年06月01日 00:14:00

该ASP通用分页类主要是易用, 速度取决于实际应用: 若已指定记录总数(intRecordCount 属性), 可支持 百万级 数据应用;若指定了统计语句(strSqlCount 属性), 则取决于该语句的统计效率; 若两者皆未指定, 则根据用户给定的查询语句, 取RecordCount属性值, 当然这样的效率就不会高了. 若您在应用中遇到问题, 请使用MSN与我联系: xiaoyuehen(at)msn.com

 适用范围:


1. 用于一般分页需求, 只需指定查询语句及其他几个主要参数即可, 方便易用.
2.分页的速度取决于你给的 SQL 语句执行效率.
3.可指定记录数统计语句, 也可以自己设置记录集总数, 灵活定制.
4.若不喜欢类产生的分页导航, 也可自行设定转向.

 

示例:


CustomPageView.asp (名称可任取)

 

<%@LANGUAGE = "VBScript" CODEPAGE="936"%>
<%Option Explicit%>
<!--#include file="Cls_PageView.asp"-->
<%
	response.Buffer = True

	Dim intDateStart
	intDateStart = Timer()

	Rem ## 打开数据库连接
	Rem #################################################################
		function f__OpenConn()
			Dim strDbPath
			Dim connstr
			strDbPath = "../db/test.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 = 10
	
	Dim arrRecordInfo, i
	Dim Conn, sql, sqlCount
	sql = "SELECT [产品ID], [产品名称], [单位]" & _
		" FROM [产品0202]" & _
		" ORDER BY [产品ID] ASC"
	sqlCount = "SELECT Count([产品ID])" & _
			" FROM [产品0202]"
	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>分页测试</title>
<style type="text/css">
<!--
.PageView {
	font-size: 12px;
}
.PageView td {
	border-right-style: solid;
	border-bottom-style: solid;
	border-right-color: #E0E0E0;
	border-bottom-color: #E0E0E0;
	border-right-width: 1px;
	border-bottom-width: 1px;
}
.PageView table {
	border-left-style: solid;
	border-top-style: solid;
	border-left-color: #E0E0E0;
	border-top-color: #E0E0E0;
	border-top-width: 1px;
	border-left-width: 1px;
}
tr.Header {
	background: #EFF7FF;
	font-size: 14px;
	font-weight: bold;
	line-height: 120%;
	text-align: center;
}
-->
</style>
<style type="text/css">
<!--
body {
	font-size: 12px;
}
a:link {
	color: #993300;
	text-decoration: none;
}
a:visited {
	color: #003366;
	text-decoration: none;
}
a:hover {
	color: #0066CC;
	text-decoration: underline;
}
a:active {
	color: #000000;
	text-decoration: none;
}
table {
	font-size: 12px;
}
-->
</style>
</head>

<body>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
  <tr>
		<td>&nbsp;<%= strPageInfo%></td>
	</tr>
</table>
<div class="PageView">
  <table width="100%" border="0" cellspacing="0" cellpadding="4">
    <tr class="Header"> 
    <td>进仓ID</td>
    <td>产品ID</td>
    <td>进仓数量</td>
  </tr>
	<%
		If IsArray(arrRecordInfo) Then
			For i = 0 to UBound(arrRecordInfo, 2)
	%>
  <tr>
    <td>&nbsp;<%= arrRecordInfo(0, i)%></td>
    <td>&nbsp;<%= arrRecordInfo(1, i)%></td>
    <td>&nbsp;<%= arrRecordInfo(2, i)%></td>
  </tr>
	<%
			Next
		End If
	%>
</table>
</div>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
  <tr> 
		<td>&nbsp;<%= strPageInfo%></td>
	</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
  <tr> 
    <td align="center">&nbsp;<%= getTimeOver(0)%></td>
  </tr>
</table>
</body>
</html>

类文件:

Cls_PageView.asp


<%
	Rem ************************************************
	Rem ** 作者: 萧月痕(xiaoyuehen)
	Rem ** ASP 通用分页类
	Rem ** 版本: 1.2.00
	Rem ** 最后修改: 2005-4-18
	Rem ** 版权说明: 在文档完整的前提下可任意复制, 传播.
	Rem ** 联系作者: xiaoyuehen(at)msn.com
	Rem ************************************************
	
	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
%>

相关文章推荐

PHP写的一个分页类

  • 2011年08月11日 14:00
  • 1KB
  • 下载

Mybatis极其(最)简(好)单(用)的一个分页插件

以前为Mybatis分页查询发愁过,而且在网上搜过很多相关的文章,最后一个都没采用。在分页的地方完全都是手写分页SQL和count的sql,总之很麻烦。 后来有一段时间想从Mybatis内部写一个分...
  • isea533
  • isea533
  • 2014年04月16日 12:32
  • 166014

这是我个人写的一个分页类

  • 2011年03月08日 14:37
  • 1KB
  • 下载

Mybatis极其(最)简(好)单(用)的一个分页插件

以前为Mybatis分页查询发愁过,而且在网上搜过很多相关的文章,最后一个都没采用。在分页的地方完全都是手写分页SQL和count的sql,总之很麻烦。 后来有一段时间想从Mybatis内部...
  • hj7jay
  • hj7jay
  • 2016年03月18日 10:00
  • 817

使用jquery编写一个分页组件(部分内容并不通用,以后需改善)

1、使用jquery编写一个分页组件     A、编写jsp数据展示页面,代码如下:   /preStage/js/css/mygridData.css"> ...

如何优雅的写一个分页代码(三)

此分页的步骤完全是按照:见我的博客http://blog.csdn.net/u014010769/article/details/45720723,这个图的步骤来写分页的 1.写PageBean类,见...

java实现的一个分页算法

一个好用的java分页算法,代码如下,只需要分页参数继承Pageable类就可以很方便分页了 package cn.com.base.common.pagination; /** * 分...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:再来一个分页类
举报原因:
原因补充:

(最多只允许输入30个字)