一、ADO版
目标:封装数据分页、导航条部分,数据显示部分不进行封装。Asp+Ac/MS SQL
<%
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'程序特点
'本程序主要是对数据分页的部分进行了封装,而数据显示部份完全由用户自定义
'支持URL多个参数
'使用说明
'参数说明
'SetPageSize 设置分页记录数
'SetConn 获取数据库链接对象
'SetSql 获取SQL查询语句
'GetRs 返回经过分页的RecordSet,此属性为只读
'方法说明
'ShowNavigation 显示分页导航条旧样式
'ShowNumNav 显示分页导航条新样式
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Class Pagination
Private intPageSize, intCursorPage, intPageCount, intRecordCount
Private objConn_Page, objrs_Page
Private strSql_Page, strLink
'==================================================================
'类初始化 设置分页默认值、取页面标记
'==================================================================
Private Sub Class_Initialize
intPageSize = 20 '//分页记录条数默认值,当没有外来分页参数时采用
'//获取当前页的值
intCursorPage = Trim(Request.QueryString("Page"))
If IsEmpty(intCursorPage) Then
intCursorPage = 1
ElseIf IsNumeric(intCursorPage) = False Then
intCursorPage =1
ElseIf CLng(intCursorPage) < 1 Then
intCursorPage =1
Else
intCursorPage = CLng(intCursorPage)
End If
End Sub
'==================================================================
'GetConn属性 获得数据库链接对象
'==================================================================
Public Property Let SetConn(objConn)
Set objConn_Page = objConn
End Property
'==================================================================
'GetSQL属性 获得查询语句
'==================================================================
Public Property Let SetSQL(strSql)
strSql_Page = strSql
End Property
'==================================================================
'PageSize属性 设置分页记录条数
'==================================================================
Public Property Let SetPageSize(intPageSizes)
If IsNumeric(intPageSizes) Then
intPageSize = CLng(intPageSizes)
Else
ShowError("PageSize参数设置错误!")
End If
End Property
'==================================================================
'GetPageSize属性 返回分页记录条数
'==================================================================
Public Property Get GetPageSize()
GetPageSize = intPageSize
End Property
'==================================================================
'GetRs属性 返回分页后的记录集
'==================================================================
Public Property Get GetRs()
Set objrs_Page = Server.CreateObject("ADODB.RecordSet")
objrs_Page.PageSize = intPageSize
objrs_Page.Open strSql_Page,objConn_Page,1,1
If Not(objrs_Page.Eof And objrs_Page.Bof) Then
intPageCount = objrs_Page.PageCount
intRecordCount = objrs_Page.RecordCount
If intCursorPage > intPageCount Then
intCursorPage = intPageCount
End If
objrs_Page.AbsolutePage = intCursorPage
End If
Set GetRs = objrs_Page
End Property
'==================================================================
'ShowNavigation属性 导航条样式一、旧式导航条
'==================================================================
Public Function ShowNavigation()
Dim strTemp
Dim BtnFirst, BtnPrev, BtnNext, BtnLast
BtnFirst = "9" '//导航条四个按钮样式
BtnPrev = "3"
BtnNext = "4"
BtnLast = ":"
strLink = GetURL()
If intRecordCount <= 0 Then
ShowError("总记录数为零")
End If
'//各个模块根据需求自行更改
strTemp = strTemp & ShowFirstPage(BtnFirst)
strTemp = strTemp & ShowPrevPage(BtnPrev)
strTemp = strTemp & ShowNumPage()
strTemp = strTemp & ShowNextPage(BtnNext)
strTemp = strTemp & ShowLastPage(BtnLast)
strTemp = strTemp & PageInfo()
ShowNavigation = strtemp
End Function
'==================================================================
'ShowFirstPage属性 创建分页导航条首页
'==================================================================
Private Function ShowFirstPage(strBtn)
If intCursorPage = 1 Then
ShowFirstPage = "<span class=""FirstPage"">"& strBtn &"</span>"
Else
ShowFirstPage = "<a href="""& strLink &"1"" class=""FirstPageA"">"& strBtn &"</a> "
End If
End Function
'==================================================================
'ShowPrevPage属性 创建分页导航条上一页
'==================================================================
Private Function ShowPrevPage(strBtn)
If intCursorPage = 1 Then
ShowPrevPage = "<span class=""PrevPage"">"& strBtn &"</span>"
Else
ShowPrevPage = "<a href="""& strLink & CStr(intCursorPage-1) & """ class=""PrevPageA"">"& strBtn &"</a> "
End If
End Function
'==================================================================
'ShowNextPage属性 创建分页导航条下一页
'==================================================================
Private Function ShowNextPage(strBtn)
If intCursorPage >= intPageCount Then
ShowNextPage = "<span class=""NextPage"">"& strBtn &"</span>"
Else
ShowNextPage = "<a href="""& strLink & CStr(intCursorPage + 1) & """ class=""NextPageA"">"& strBtn &"</a> "
End If
End Function
'==================================================================
'ShowLastPage属性 创建分页导航条末页
'==================================================================
Private Function ShowLastPage(strBtn)
If intCursorPage >= intPageCount Then
ShowLastPage = "<span class=""LastPage"">"& strBtn &"</span>"
Else
ShowLastPage = "<a href="""& strLink & CStr(intPageCount) & """ class=""LastPageA"">"& strBtn &"</a> "
End If
End Function
'==================================================================
'PageInfo属性 分页信息,可自行修改
'==================================================================
Private Function PageInfo()
PageInfo = "页次:"& intCursorPage &"/"& intPageCount &" 共"& intRecordCount &"条记录 "& intPageSize &"条/每页"
End Function
'==================================================================
'ShowNumPage属性 创建决普通模式数字导航条
'==================================================================
Private Function ShowNumPage()
Dim intViewPage, intOffSet, intFormPage, intToPage, intI
Dim strTemp
intViewPage = 8 '//显示的分页页数
intOffSet = 4 '//分页偏移量
If intRecordCount > intPageSize Then
intFormPage = intCursorPage - intOffSet
intToPage = intCursorPage + intViewPage - intOffSet -1
If intViewPage > intPageCount Then
intFormPage = 1
intToPage = intPageCount
Else
If intFormPage < 1 Then
intFormPage = 1
intToPage = intCursorPage + 1 -intFormPage
If (intToPage - intFormPage) < intViewPage And (intToPage - intFormPage) < intPageCount Then intToPage = intViewPage
ElseIf intToPage > intPageCount Then
intFormPage = intCursorPage - intPageCount + intToPage
intToPage = intPageCount
If (intToPage - intFormPage) < intViewPage And (intToPage-intFormPage) < intPageCount Then intFormPage = intPageCount - intViewPage + 1
End If
End If
For intI = intFormPage To intToPage
If intI <> intCursorPage Then
strTemp = strTemp & "<a href="""& strLink & CStr(intI) &""" class=""NumPage"">["& CStr(intI) &"]</a>"
Else
strTemp = strTemp & "<span class=""CursorPage"">["& intI &"]</span>"
End If
Next
End If
ShowNumPage = strTemp
End Function
'==================================================================
'ShowNumNav属性 导航条样式二、数字导航条
'==================================================================
Public Function ShowNumNav()
Dim intViewPage, intOffSet, intFormPage, intToPage, intI
Dim strTemp
strLink = GetURL()
intViewPage = 10 '//显示的分页页数
intOffSet = 2 '//分页偏移量
If intRecordCount > intPageSize Then
intFormPage = intCursorPage - intOffSet
intToPage = intCursorPage + intViewPage - intOffSet -1
If intViewPage > intPageCount Then
intFormPage = 1
intToPage = intPageCount
Else
If intFormPage < 1 Then
intFormPage = 1
intToPage = intCursorPage + 1 -intFormPage
If (intToPage - intFormPage) < intViewPage And (intToPage - intFormPage) < intPageCount Then intToPage = intViewPage
ElseIf intToPage > intPageCount Then
intFormPage = intCursorPage - intPageCount + intToPage
intToPage = intPageCount
If (intToPage - intFormPage) < intViewPage And (intToPage-intFormPage) < intPageCount Then intFormPage = intPageCount - intViewPage + 1
End If
End If
strTemp = "<a href="""& strLink &"1""><<</a> "
For intI = intFormPage To intToPage
If intI <> intCursorPage Then
strTemp = strTemp & "<a href="""& strLink & intI &""">"& intI &"</a> | "
Else
strTemp = strTemp & "<span class=""CursorPage"">"& intI &"</span> | "
End If
Next
If intPageCount > intViewPage Then '//总页数是否大于显示页数
strTemp = strTemp & "... <a href="""& strLink & intPageCount &""">"& intPageCount &" >></a> <input name=""CursorPage"" type=""text"" id=""CursorPage"" size=""2"" maxlength=""4"" value="""& intCursorPage &""" class=""Page_input"" onMouseOver=""this.focus()"" onFocus=""this.select()"" onKeyDown=""javascript: if(window.event.keyCode == 13) window.location='"& strLink &"'+this.value;"" />"
Else
strTemp = strTemp & " <a href="""& strLink & intPageCount &""">>></a>"
End If
End If
ShowNumNav = strTemp
End Function
'==================================================================
'GetURL属性 返回URL字串及页面参数
'==================================================================
Private Function GetURL()
Dim strUrl, strFiles, strSearch, strParameter, strResult
Dim intI
strUrl = Split(Request.ServerVariables("URL"),"/")
strFiles = strUrl(UBound(strUrl,1)) '//获取当前URL中的文件名
strSearch = "Page=" '//检索字串
strParameter = Trim(Request.ServerVariables("QUERY_STRING")) '//获取参数
If strParameter = "" Then '//没有参数
strResult = strFiles & "?Page="
Else
If InstrRev(strParameter,strSearch) = 0 Then '//没找到检索字串
strResult = strFiles & "?" & strParameter & "&Page="
Else
intI = InstrRev(strParameter,strSearch)-2
If intI = -1 Then '//带Page参数
strResult = strFiles & "?Page="
Else
strParameter = Left(strParameter,intI) '//带多参数
strResult = strFiles & "?" & strParameter &"&Page="
End If
End If
End If
GetURL = strResult
End Function
'==================================================================
'设置Class Terminate事件
'==================================================================
Private Sub Class_Terminate
objrs_Page.Close
Set objrs_Page = Nothing
End Sub
'==================================================================
'ShowError属性 错误提示
'==================================================================
Private Sub ShowError(strError)
Response.Write(strError)
Response.End()
End Sub
End Class
%>
目标:封装数据分页、导航条部分,数据显示部分不进行封装。Asp+Ac/MS SQL
<%
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'程序特点
'本程序主要是对数据分页的部分进行了封装,而数据显示部份完全由用户自定义
'支持URL多个参数
'使用说明
'参数说明
'SetPageSize 设置分页记录数
'SetConn 获取数据库链接对象
'SetSql 获取SQL查询语句
'GetRs 返回经过分页的RecordSet,此属性为只读
'方法说明
'ShowNavigation 显示分页导航条旧样式
'ShowNumNav 显示分页导航条新样式
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Class Pagination
Private intPageSize, intCursorPage, intPageCount, intRecordCount
Private objConn_Page, objrs_Page
Private strSql_Page, strLink
'==================================================================
'类初始化 设置分页默认值、取页面标记
'==================================================================
Private Sub Class_Initialize
intPageSize = 20 '//分页记录条数默认值,当没有外来分页参数时采用
'//获取当前页的值
intCursorPage = Trim(Request.QueryString("Page"))
If IsEmpty(intCursorPage) Then
intCursorPage = 1
ElseIf IsNumeric(intCursorPage) = False Then
intCursorPage =1
ElseIf CLng(intCursorPage) < 1 Then
intCursorPage =1
Else
intCursorPage = CLng(intCursorPage)
End If
End Sub
'==================================================================
'GetConn属性 获得数据库链接对象
'==================================================================
Public Property Let SetConn(objConn)
Set objConn_Page = objConn
End Property
'==================================================================
'GetSQL属性 获得查询语句
'==================================================================
Public Property Let SetSQL(strSql)
strSql_Page = strSql
End Property
'==================================================================
'PageSize属性 设置分页记录条数
'==================================================================
Public Property Let SetPageSize(intPageSizes)
If IsNumeric(intPageSizes) Then
intPageSize = CLng(intPageSizes)
Else
ShowError("PageSize参数设置错误!")
End If
End Property
'==================================================================
'GetPageSize属性 返回分页记录条数
'==================================================================
Public Property Get GetPageSize()
GetPageSize = intPageSize
End Property
'==================================================================
'GetRs属性 返回分页后的记录集
'==================================================================
Public Property Get GetRs()
Set objrs_Page = Server.CreateObject("ADODB.RecordSet")
objrs_Page.PageSize = intPageSize
objrs_Page.Open strSql_Page,objConn_Page,1,1
If Not(objrs_Page.Eof And objrs_Page.Bof) Then
intPageCount = objrs_Page.PageCount
intRecordCount = objrs_Page.RecordCount
If intCursorPage > intPageCount Then
intCursorPage = intPageCount
End If
objrs_Page.AbsolutePage = intCursorPage
End If
Set GetRs = objrs_Page
End Property
'==================================================================
'ShowNavigation属性 导航条样式一、旧式导航条
'==================================================================
Public Function ShowNavigation()
Dim strTemp
Dim BtnFirst, BtnPrev, BtnNext, BtnLast
BtnFirst = "9" '//导航条四个按钮样式
BtnPrev = "3"
BtnNext = "4"
BtnLast = ":"
strLink = GetURL()
If intRecordCount <= 0 Then
ShowError("总记录数为零")
End If
'//各个模块根据需求自行更改
strTemp = strTemp & ShowFirstPage(BtnFirst)
strTemp = strTemp & ShowPrevPage(BtnPrev)
strTemp = strTemp & ShowNumPage()
strTemp = strTemp & ShowNextPage(BtnNext)
strTemp = strTemp & ShowLastPage(BtnLast)
strTemp = strTemp & PageInfo()
ShowNavigation = strtemp
End Function
'==================================================================
'ShowFirstPage属性 创建分页导航条首页
'==================================================================
Private Function ShowFirstPage(strBtn)
If intCursorPage = 1 Then
ShowFirstPage = "<span class=""FirstPage"">"& strBtn &"</span>"
Else
ShowFirstPage = "<a href="""& strLink &"1"" class=""FirstPageA"">"& strBtn &"</a> "
End If
End Function
'==================================================================
'ShowPrevPage属性 创建分页导航条上一页
'==================================================================
Private Function ShowPrevPage(strBtn)
If intCursorPage = 1 Then
ShowPrevPage = "<span class=""PrevPage"">"& strBtn &"</span>"
Else
ShowPrevPage = "<a href="""& strLink & CStr(intCursorPage-1) & """ class=""PrevPageA"">"& strBtn &"</a> "
End If
End Function
'==================================================================
'ShowNextPage属性 创建分页导航条下一页
'==================================================================
Private Function ShowNextPage(strBtn)
If intCursorPage >= intPageCount Then
ShowNextPage = "<span class=""NextPage"">"& strBtn &"</span>"
Else
ShowNextPage = "<a href="""& strLink & CStr(intCursorPage + 1) & """ class=""NextPageA"">"& strBtn &"</a> "
End If
End Function
'==================================================================
'ShowLastPage属性 创建分页导航条末页
'==================================================================
Private Function ShowLastPage(strBtn)
If intCursorPage >= intPageCount Then
ShowLastPage = "<span class=""LastPage"">"& strBtn &"</span>"
Else
ShowLastPage = "<a href="""& strLink & CStr(intPageCount) & """ class=""LastPageA"">"& strBtn &"</a> "
End If
End Function
'==================================================================
'PageInfo属性 分页信息,可自行修改
'==================================================================
Private Function PageInfo()
PageInfo = "页次:"& intCursorPage &"/"& intPageCount &" 共"& intRecordCount &"条记录 "& intPageSize &"条/每页"
End Function
'==================================================================
'ShowNumPage属性 创建决普通模式数字导航条
'==================================================================
Private Function ShowNumPage()
Dim intViewPage, intOffSet, intFormPage, intToPage, intI
Dim strTemp
intViewPage = 8 '//显示的分页页数
intOffSet = 4 '//分页偏移量
If intRecordCount > intPageSize Then
intFormPage = intCursorPage - intOffSet
intToPage = intCursorPage + intViewPage - intOffSet -1
If intViewPage > intPageCount Then
intFormPage = 1
intToPage = intPageCount
Else
If intFormPage < 1 Then
intFormPage = 1
intToPage = intCursorPage + 1 -intFormPage
If (intToPage - intFormPage) < intViewPage And (intToPage - intFormPage) < intPageCount Then intToPage = intViewPage
ElseIf intToPage > intPageCount Then
intFormPage = intCursorPage - intPageCount + intToPage
intToPage = intPageCount
If (intToPage - intFormPage) < intViewPage And (intToPage-intFormPage) < intPageCount Then intFormPage = intPageCount - intViewPage + 1
End If
End If
For intI = intFormPage To intToPage
If intI <> intCursorPage Then
strTemp = strTemp & "<a href="""& strLink & CStr(intI) &""" class=""NumPage"">["& CStr(intI) &"]</a>"
Else
strTemp = strTemp & "<span class=""CursorPage"">["& intI &"]</span>"
End If
Next
End If
ShowNumPage = strTemp
End Function
'==================================================================
'ShowNumNav属性 导航条样式二、数字导航条
'==================================================================
Public Function ShowNumNav()
Dim intViewPage, intOffSet, intFormPage, intToPage, intI
Dim strTemp
strLink = GetURL()
intViewPage = 10 '//显示的分页页数
intOffSet = 2 '//分页偏移量
If intRecordCount > intPageSize Then
intFormPage = intCursorPage - intOffSet
intToPage = intCursorPage + intViewPage - intOffSet -1
If intViewPage > intPageCount Then
intFormPage = 1
intToPage = intPageCount
Else
If intFormPage < 1 Then
intFormPage = 1
intToPage = intCursorPage + 1 -intFormPage
If (intToPage - intFormPage) < intViewPage And (intToPage - intFormPage) < intPageCount Then intToPage = intViewPage
ElseIf intToPage > intPageCount Then
intFormPage = intCursorPage - intPageCount + intToPage
intToPage = intPageCount
If (intToPage - intFormPage) < intViewPage And (intToPage-intFormPage) < intPageCount Then intFormPage = intPageCount - intViewPage + 1
End If
End If
strTemp = "<a href="""& strLink &"1""><<</a> "
For intI = intFormPage To intToPage
If intI <> intCursorPage Then
strTemp = strTemp & "<a href="""& strLink & intI &""">"& intI &"</a> | "
Else
strTemp = strTemp & "<span class=""CursorPage"">"& intI &"</span> | "
End If
Next
If intPageCount > intViewPage Then '//总页数是否大于显示页数
strTemp = strTemp & "... <a href="""& strLink & intPageCount &""">"& intPageCount &" >></a> <input name=""CursorPage"" type=""text"" id=""CursorPage"" size=""2"" maxlength=""4"" value="""& intCursorPage &""" class=""Page_input"" onMouseOver=""this.focus()"" onFocus=""this.select()"" onKeyDown=""javascript: if(window.event.keyCode == 13) window.location='"& strLink &"'+this.value;"" />"
Else
strTemp = strTemp & " <a href="""& strLink & intPageCount &""">>></a>"
End If
End If
ShowNumNav = strTemp
End Function
'==================================================================
'GetURL属性 返回URL字串及页面参数
'==================================================================
Private Function GetURL()
Dim strUrl, strFiles, strSearch, strParameter, strResult
Dim intI
strUrl = Split(Request.ServerVariables("URL"),"/")
strFiles = strUrl(UBound(strUrl,1)) '//获取当前URL中的文件名
strSearch = "Page=" '//检索字串
strParameter = Trim(Request.ServerVariables("QUERY_STRING")) '//获取参数
If strParameter = "" Then '//没有参数
strResult = strFiles & "?Page="
Else
If InstrRev(strParameter,strSearch) = 0 Then '//没找到检索字串
strResult = strFiles & "?" & strParameter & "&Page="
Else
intI = InstrRev(strParameter,strSearch)-2
If intI = -1 Then '//带Page参数
strResult = strFiles & "?Page="
Else
strParameter = Left(strParameter,intI) '//带多参数
strResult = strFiles & "?" & strParameter &"&Page="
End If
End If
End If
GetURL = strResult
End Function
'==================================================================
'设置Class Terminate事件
'==================================================================
Private Sub Class_Terminate
objrs_Page.Close
Set objrs_Page = Nothing
End Sub
'==================================================================
'ShowError属性 错误提示
'==================================================================
Private Sub ShowError(strError)
Response.Write(strError)
Response.End()
End Sub
End Class
%>