asp的通用数据分页类

时间:2021-05-28

(原创)<!--#includefile="Conn.asp"-->
通用数据分页类
通用分页类,以后写分页显示数据时就轻松多啦.直接调用此类,然后再Execute即可以取得当前页的所有数据.
此类所做的工作是只取得当前页的数据,和总页数和总记录数等等数据.

ASP代码:
<%
'/*****************************分页显示类**************************
'/*作者:哇哇鱼
'/*日期:2004年11月18日
'/*作用:取得某一页的数据并返回给外部
'/*说明示例:
'/*DimMyPage=NewPageClass
'/*MyPage.Conn=Conn'设置连接对象
'/*MyPage.PageSize=20'设置一页显示多少条数据(默认为10条)
'/*MyPage.CurPage=2'设置当前要显示的页码
'/*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'/*MyPage.TableName="Member"'设置表名
'/*MyPage.Fields="ID,MemberName,MemberPass"'设置显示字段列表
'/*MyPage.Condition="ID>100"'设置查询条件
'/*MyPage.OrderBy="IDDESC"'设置排序条件(一定要设置该属性)
'/*SetPageRs=MyPage.Execute'返回当前第2页的数据(RecordSet对象),如果出错则返回Nothing值
'/*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'/*'以上的定义也可以用以下的方法:ExecuteBy("表名","字段列表","查询条件","排序条件")
'/*SetPageRs=MyPage.ExecuteBy("Member","ID,MemberName,MemberPass","ID>100","IDDESC")
'/*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'/*PageCount=MyPage.PageCount'返回页码总数
'/*RecordCount=MyPage.RecordCount'返回记录总数
'/*NextPage=MyPage.NextPage'返回下页的页码
'/*PrePage=MyPage.PrePage'返回上一页的页码
'/*****************************************************************
ClassPageClass
PrivateConnection'连接数据库的外部Connection对象
PrivateRs
PrivateList_Fields
PrivateTable_Name
PrivateQuery_Where
PrivateOrderBy_SQL'字段排序语句部分
PrivatePage_Count'返回当前查询的记录页总数
PrivatePage_Size'设置一页显示多少条的记录
PrivateCur_Page'设置当前的页码
PrivateRecord_Count'返回当前查询的记录总数

'/****************设置Connection对象****************************
PublicPropertyLetConn(ByRefObjConn)
SetConnection=ObjConn
EndProperty
PublicPropertyGetConn()
SetConn=Connection
EndProperty
'/****************End******************************************

'/****************设置查询SQL语句*******************************
''查询显示字段
PublicPropertyLetFields(ByValValue)
List_Fields=Value
EndProperty
PublicPropertyGetFields()
Fields=List_Fields
EndProperty
''查询表名
PublicPropertyLetTableName(ByValValue)
Table_Name=Value
EndProperty
PublicPropertyGetTableName()
TableName=Table_Name
EndProperty
''查询条件
PublicPropertyLetCondition(ByValValue)
Query_Where=Value
EndProperty
PublicPropertyGetCondition()
Condition=Query_Where
EndProperty
''*****************排序部分********************************************
''Value语不用写上OrderBy。如:[object].OrderBy="IDDesc,PostTimeAsc"
PublicPropertyLetOrderBy(ByValValue)
OrderBy_SQL=Value
EndProperty
PublicPropertyGetOrderBy()
OrderBy=OrderBy_SQL
EndProperty
'/****************End******************************************

'/****************返回当前查询结果的总页数***********************
PublicPropertyGetPageCount()
PageCount=Page_Count
EndProperty
PublicPropertyGetRecordCount()
RecordCount=Record_Count
EndProperty
PublicPropertyGetNextPage()
IfCur_Page<Page_CountThen
NextPage=Cur_Page+1
Else
NextPage=Page_Count
EndIf
EndProperty
PublicPropertyGetPrePage()
IfCur_Page>1Then
PrePage=Cur_Page-1
Else
PrePage=Cur_Page
EndIf
EndProperty
'/****************End******************************************

'/****************设置一页显示的记录数***************************
PublicPropertyLetPageSize(ByValValue)
IfNotIsNumeric(Value)OrValue=""Then
Value=10
Else
Value=Cint(Value)
EndIf
IfValue<1ThenValue=10
Page_Size=Value
EndProperty
PublicPropertyGetPageSize()
PageSize=Page_Size
EndProperty
''设置当前的页码数**************************
PublicPropertyLetPage(ByValValue)
IfNotIsNumeric(Value)OrValue=""Then
Value=1
Else
Value=CLng(Value)
EndIf
IfValue<1ThenValue=1
Cur_Page=Value
EndProperty
PublicPropertyGetPage()
Page=Cur_Page
EndProperty
'/****************End******************************************

PrivateSubClass_Initialize
'初始化RecordSet对象
Page_Size=10'默认一页为10条数据
CurPage=1'默认当前为第一页
Record_Count=0
Page_Count=0
EndSub

PrivateSubClass_Terminate
CallCloseRecordSet
EndSub

'/***关闭数据库的连接*******
PrivateSubCloseRecordSet
OnErrorResumeNext
IfIsObject(Rs)Then
Rs.Close
SetRs=Nothing
EndIf
OnErrorGoto0
EndSub

'/**********执行查询返回对应页码的数据***********************************************
PublicFunctionExecuteBy(ByValoTableName,ByValoFields,ByValoCondition,ByValoOrderBy)
Table_Name=oTableName
List_Fields=oFields
Query_Where=oCondtion
OrderBy_SQL=oOrderBy
SetExecuteBy=Execute()
EndFunction
'查询并返回当前CurPage的页码记录
PublicFunctionExecute()
CallCloseRecordSet
OnErrorResumeNext
DimTSQL,TopMod,sWhere
IfNotIsObject(Connection)OrTable_Name=""OrOrderBy_SQL=""Then
SetExecute=Nothing
Record_Count=0
Page_Count=0
ExitFunction
EndIf
IfTrim(Query_Where)<>""Then
sWhere="Where"&Query_Where
Else
sWhere=""
EndIf
TSQL="SelectCount(*)From["&Table_Name&"]"&sWhere
Record_Count=Connection.Execute(TSQL)(0)'获取记录总数
IfErrThen
Err.Clear
SetExecute=Nothing
Record_Count=0
Page_Count=0
ExitFunction
EndIf
IfRecord_Count<1Then
SetExecute=Nothing
Record_Count=0
Page_Count=0
ExitFunction
EndIf
'取得页的总数
IfRecord_CountModPage_Size<>0Then
TopMod=Record_CountModPage_Size
Page_Count=Fix(Record_Count/Page_Size)+1
IfCur_Page<Page_CountThen
TopMod=Page_Size
EndIf
Else
TopMod=Page_Size
Page_Count=Fix(Record_Count/Page_Size)
EndIf
IfCur_Page>Page_CountThenCur_Page=Page_Count
IfCur_Page<1ThenCur_Page=1
IfTrim(List_Fields)=""ThenList_Fields="*"
TSQL="Select*From(SelectTop"&TopMod&"*From(SelectTop"&(Cur_Page*Page_Size)&""&List_Fields&"From["&Table_Name&"]"&sWhere&"OrderBy"&OrderBy_SQL&")OrderBy"&TransformOrder(OrderBy_SQL)&")OrderBy"&OrderBy_SQL
SetRs=Connection.Execute(TSQL)
IfErrThen
Err.Clear
SetExecute=Nothing
Record_Count=0
Page_Count=0
ExitFunction
EndIf
SetExecute=Rs
EndFunction

'转换OrderBy的顺序ASC->DESCDESC->ASC
PrivateFunctionTransformOrder(ByValValue)
IfValue=""Then
TransformOrder=""
ExitFunction
EndIf
DimOrderArray,i,Result,ByString,Fields,InPos
OrderArray=Split(Value,",")'分解每个字段值
Fori=0ToUbound(OrderArray)
IfOrderArray(i)<>""Then
InPos=InStrRev(Trim(OrderArray(i)),"")'找出排序的顺序
IfInPos<1Then'如果找不到则是ASC排序
ByString="ASC"
Fields=OrderArray(i)+""
Else
ByString=Trim(Mid(OrderArray(i),InPos+1))
Fields=Left(OrderArray(i),InPos)
IfByString<>""Then
ByString=UCase(ByString)
Else
ByString="ASC"
EndIf
EndIf
''转换排序
IfByString="ASC"Then
ByString="DESC"
Else
ByString="ASC"
EndIf
Result=Result+Fields+ByString+","
EndIf
Next
IfResult<>""ThenResult=Left(Result,Len(Result)-1)
TransformOrder=Result
EndFunction
EndClass


'示例代码:
SubShow_List
DimPage,PageRs
Page=Request("Page")
DimMyPage
SetMyPage=NewPageClass
MyPage.Conn=Conn
MyPage.PageSize=20
MyPage.Page=Page
MyPage.TableName="table1"
MyPage.Fields="*"
MyPage.OrderBy="IDAsc"
SetPageRs=MyPage.Execute
'SetPageRs=MyPage.ExecuteBy("table1","*","","IDAsc")
IfPageRsIsNothingThenExitSub
DoUntilPageRs.Eof
Response.Write"<trbgcolor=""#FDFDFD""style=""cursor:hand""onmouseover=""this.style.background='#F3F3F3'""onmouseout=""this.style.background='#FDFDFD'"">"
Response.Write"<tdheight=""20""><divalign=""center"">"&PageRs("ID")&"</div></td>"
Response.Write"<td>"&PageRs("aaaa")&"</td>"
Response.Write"<td><ahref="""&PageRs("bbbb")&"""><fontcolor='#000000'>"&PageRs("bbbb")&"</font></a></td>"
Response.Write"<td>"&PageRs("cccc")&"</td>"
Response.Write"</tr>"
PageRs.MoveNext
Loop
PageRs.Close
PageCount=MyPage.PageCount
Page=MyPage.Page'取得当前正确的页码数
NextPage=MyPage.NextPage
PrePage=MyPage.PrePage
SetPageRs=Nothing
SetMyPage=Nothing
EndSub
Show_List
%>




声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章