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

热门教程

asp 完整分页类实例实用程序

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

<%
Class xuduoPage
 '************************************
 '变量定义
 '************************************
 'xConn ---- 数据库教程连接
 'xRs ---- 记录集
 'xTmp -- 存放最终记录的数组
 'xCountSql ---- 查询总记录数SQL语句
 'xSql ---- 查询SQL语句
 'xPageSize ---- 分页大小
 'xPage ---- 当前页
 'xRecordCount ---- 记录数
 'xPageCount ---- 总页数
 Private xConn,xRs,xTmp,xTableName,xFields,xWhere,xOrder,xRef,xOrderType,xPrimaryKey,xCountSql,xSql,xPageSize,xPage,xRecordCount,xPageCount
 Private xFlag,xMiddleIndex,xBar,xI,xPageStart,xPageEnd
 
 '************************************
 '类初始化
 '************************************
 Private Sub Class_Initialize()
  xTableName = ""
  xFields = "*"
  xWhere = ""
  xOrderType = 1
  xPageSize = 10
  xBar = 1
 End Sub
 
 '************************************
 '类结束
 '************************************
 Private Sub Class_Terminate()
 
 End Sub

 '************************************
 'SetConn 得到数据库连接www.111com.net
 '************************************
 Public Property Let SetConn(value)
  Set xConn = value
 End Property

 '************************************
 'TableName 得到表名
 '************************************
 Public Property Let TableName(value)
  xTableName = value
 End Property
 
 '************************************
 'SetFields 得到查询字段
 '************************************
 Public Property Let SetFields(value)
  xFields = value
 End Property
 
 '************************************
 'SetWhere 得到其它条件
 '************************************
 Public Property Let SetWhere(value)
  xWhere = value
 End Property
 
 
 '************************************
 'SetWhere 得到其它条件
 '************************************
 Public Property Let SetRef(value)
  xRef = value
 End Property
 
 '************************************
 'SetOrder 得到排序类型
 '************************************
 Public Property Let SetOrder(value)
  xOrderType = value
 End Property
 
 '************************************
 'SetPKey 得到主键
 '************************************
 Public Property Let SetPKey(value)
  xPrimaryKey = value
 End Property
 
 '************************************
 'GetPageSize 得到页面分页尺寸
 '************************************
 Public Property Let SetPageSize(value)
  xPageSize = value
 End Property
 
 '************************************
 'SetBar 得到页面分页尺寸
 '************************************
 Public Property Let SetBar(value)
  xBar = value
 End Property

 '************************************
 'GetRecCount 求总记录数
 '************************************
 Private Function GetRecCount()
  If xWhere = "" Then
   'xCountSql = "Select Count(*) From [" & xTableName & "]"
   xCountSql = "Select Count(*) From " & xTableName
  Else
   'xCountSql = "Select Count(*) From [" & xTableName & "] Where " & xWhere
   xCountSql = "Select Count(*) From " & xTableName & " Where " & xWhere
  End If
  rem response.Write(xCountSql)
  GetRecCount = xConn.Execute(xCountSql)(0)
 End Function
 
 '************************************
 'GetPageCount 求总页数
 '************************************
 Private Function GetPageCount(iRecordCount)
  If iRecordCount Mod xPageSize = 0 Then
   GetPageCount = iRecordCountxPageSize
  Else
   GetPageCount = iRecordCountxPageSize+1
  End If
 End Function
 
 '************************************
 'GetMidPageIndex 计算中间页的页索引
 '************************************
 Private Function GetMidPageIndex()
  GetMidPageIndex = (xPageCount)-1
 End Function
 
 '************************************
 'GetPage 求当前页
 '************************************
 Private Function GetPage(iPageCount)
  Dim iPage
  iPage = Request("page")
  If iPage = "" Then xPage = "1"
  If Not Isnumeric(iPage) Then iPage = "1"
  iPage = CLng(iPage)
  If iPage > iPageCount Then iPage = iPageCount
  If iPage < 1 Then iPage = 1
  GetPage = iPage
 End Function
 
 '************************************
 'GetOrderString 得到排序语句
 '************************************
 Private Function GetOrderString()
  If xOrderType = 1 Then
   GetOrderString = " Order By " & xPrimaryKey & " Asc"
  ElseIf xOrderType = 2  Then
   GetOrderString = " Order By " & xPrimaryKey & " Desc"
  Else
   GetOrderString = ""
  End If
 End Function

 '************************************
 'GetWhereString 得到条件语句
 '************************************
 Private Function GetWhereString()
  If xWhere <> "" Then
   GetWhereString = GetWhereString & " Where " & xWhere
  Else 
   GetWhereString = ""
  End If
 End Function

 '************************************
 'GetSQLString 生成SQL
 '************************************
 Private Function GetSQLString()
  Dim xWhere_,xMiddleIndex
  xWhere_ = GetWhereString()'//生成条件语句
  xMiddleIndex = GetMidPageIndex()'//得到中间页索引
  'Response.Write("xMiddleIndex=" & xMiddleIndex)
  'Response.Write("
")
  If xOrderType = 1 Then'//升序
   If xPage=1 Then
    GetSQLString = "Select Top " & xPageSize & " " & xFields & " From " & xTableName & " " & xWhere_ & " Order By " & xPrimaryKey & " Asc"
    xFlag = "11"
   ElseIf 1 < xPage And xPage <= xMiddleIndex Then
    GetSQLString = "Select Top " & xPageSize & " " & xFields & " From " & xTableName & " Where " & xPrimaryKey & " > (Select Max(" & xPrimaryKey & ") From (Select Top " & xPagesize*(xPage-1) & " " & xPrimaryKey & " From " & xTableName & " " & xWhere_ & " Order By " & xPrimaryKey & " Asc) TableA) And " & xWhere & " Order By " & xPrimaryKey & " Asc"
    xFlag = "12"
   ElseIf xMiddleIndex < xPage And xPage < xPageCount  Then
    GetSQLString = "Select * From (Select Top " & xPageSize & " " & xFields & " From " & xTableName & " Where " & xPrimaryKey & " < (Select Min(" & xPrimaryKey & ") From (Select Top " & xRecordCount-xPagesize*xPage & " " & xPrimaryKey & " From " & xTableName & " " & xWhere_ & " Order By " & xPrimaryKey & " Desc) TableA) And " & xWhere & " Order By " & xPrimaryKey & " Desc) TableB Order By " & xPrimaryKey & " Asc"
    xFlag = "13"
   ElseIf xPage=xPageCount Then
    GetSQLString = "Select * From (Select Top " & xRecordCount-xPageSize*(xPageCount-1) & " " & xFields & " From " & xTableName & " " & xWhere_ & " Order By " & xPrimaryKey & " Desc) TableA Order By " & xPrimaryKey & " Asc"
    xFlag = "14"
   End If
  Else'//降序
   If xPage=1 Then
    GetSQLString = "Select Top " & xPageSize & " " & xFields & " From " & xTableName & " " & xWhere_ &" Order By " & xPrimaryKey & " Desc"
    xFlag = "21"
   ElseIf 1 < xPage And xPage <= xMiddleIndex Then
    GetSQLString = "Select Top " & xPageSize & " " & xFields & " From " & xTableName & " Where " & xPrimaryKey & " < (Select Min(" & xPrimaryKey & ") From (Select Top " & xPagesize*(xPage-1) & " " & xPrimaryKey & " From " & xTableName & " " & xWhere_ & " Order By " & xPrimaryKey & " Desc) TableA) And " & xWhere & " Order By " & xPrimaryKey & " Desc"
    xFlag = "22"
   ElseIf xMiddleIndex < xPage And xPage < xPageCount  Then
    GetSQLString = "Select * From (Select Top " & xPageSize & " " & xFields & " From " & xTableName & " Where " & xPrimaryKey & " > (Select Max(" & xPrimaryKey & ") From (Select Top " & xRecordCount-xPagesize*xPage & " " & xPrimaryKey & " From " & xTableName & " " & xWhere_ & " Order By " & xPrimaryKey & " Asc) TableA) And " & xWhere & " Order By " & xPrimaryKey & " Asc) TableB Order By " & xPrimaryKey & " Desc"
    xFlag = "23"
   ElseIf xPage=xPageCount Then
    GetSQLString = "Select * From (Select Top " & xRecordCount-xPageSize*(xPageCount-1) & " " & xFields & " From " & xTableName & " " & xWhere_ & " Order By " & xPrimaryKey & " Asc) As TableA Order By " & xPrimaryKey & " Desc"
    xFlag = "24"
   End If
  End If
 End Function
 
 '************************************
 'GetArray 得到记录数组
 '************************************
 Public Function GetArray()
  Set xRs = Server.CreateObject("Adodb.RecordSet")
  xRecordCount = GetRecCount()'//计算总记录数
  xPageCount = GetPageCount(xRecordCount) '//计算总页数
  xPage = GetPage(xPageCount)'//计算当前页
  xSql = GetSqlString()
  xRs.Open xSql,xConn,1,1
  If Not xRs.Eof Then
   xTmp = xRs.GetRows()
  Else
   xTmp = Null
  End If
  xRs.Close:Set xRs = Nothing
  GetArray = xTmp
 End Function

 '************************************
 'GetURL 返回当前URL,保证URL参数不出错www.111com.net
 '************************************
 Private Function GetURL()
  Dim PageUrl,FileNameStr,FileName,oI,oJ,PageStr,ResultUrl,TempUrl,T
  PageStr = "page="
  FileNameStr = Request.Servervariables("Url")
  FileNameStr = Split(FileNameStr,"/")
  oI = Ubound(FileNameStr,1)
  FileName = FileNameStr(oI)'得到当前页文件名

  PageUrl = Trim(Request.Servervariables("Query_string"))
  '--西瓜增加
        If xRef And Cstr(Request.Form)<>"" Then PageUrl = PageUrl + Request.Form
  '--结束
        If PageUrl <> "" Then
            If InStr(PageUrl,PageStr)>0 Then
    If InStr(PageUrl,"&")>0 Then
     TempUrl = Split(PageUrl,"&")
     PageUrl = ""
     For T = 0 To Ubound(TempUrl)
      If InStr(TempUrl(T),PageStr)=0 Then
          PageUrl = PageUrl & "&" & TempUrl(T)
      End If
     Next
     PageUrl = Right(PageUrl,Len(PageUrl)-1)
     ResultUrl = FileName & "?" & PageUrl &"&page="
    Else
     ResultUrl = FileName & "?page="
    End If
   Else
    ResultUrl = FileName & "?" & PageUrl &"&page="
            End If
  Else
   ResultUrl = FileName & "?page="
  End If
  GetURL = ResultUrl
 End Function


 '************************************
 'CurrentPage 返回当前页码
 '************************************
 Public Property Get CurrentPage
  CurrentPage = xPage
 End Property
 
 '************************************
 'CountPage 返回总页数
 '************************************
 Public Property Get CountPage
  CountPage = xPageCount
 End Property
 
 '************************************
 'CountRecord 返回总记录数www.111com.net
 '************************************
 Public Property Get CountRecord
  CountRecord = xRecordCount
 End Property
 
 Public Function ShowBar()
  Dim xPageTmp,xPageStart,xPageEnd,xBarHtmls,xPageUrl,xSelected
  xSelected = ""
  xPageUrl = GetURL()
  If xPageCount<10 And xPageCount>0 Then
   xPageStart = 1
   xPageEnd = xPageCount
  Else
   xPageStart = xPage-5
   xPageEnd = xPage+4
   If xPageStart<=0 Then
    xPageStart = 1
    xPageEnd = 10
   End If
   If xPageEnd>=xPageCount Then
    xPageStart = xPageCount-10
    xPageEnd = xPageCount
   End If
  End If
  If xBar=1 Then
   For xI = xPageStart To xPageEnd
    If xI = xPage Then
     xPageTmp = xPageTmp & " " & xI & " "
    Else
     xPageTmp = xPageTmp & " " & xI & " "
    End If
   Next
   xBarHtmls = "" 
   xBarHtmls = xBarHtmls & "

"
    xBarHtmls = xBarHtmls & ""
      xBarHtmls = xBarHtmls & ""
     xBarHtmls = xBarHtmls & ""
     xBarHtmls = xBarHtmls & ""
     xBarHtmls = xBarHtmls & ""
      xBarHtmls = xBarHtmls & ""
      rem xBarHtmls = xBarHtmls & ""
    xBarHtmls = xBarHtmls & ""
   xBarHtmls = xBarHtmls & "
共 " & xPageCount & "页  " & xRecordCount & "条记录  当前第" & xPage & "页   首页"
     If xPage>1 Then
      xBarHtmls = xBarHtmls & " 上一页"
     Else
      xBarHtmls = xBarHtmls & " 上一页"
     End If
     xBarHtmls = xBarHtmls & xPageTmp
     If xPage       xBarHtmls = xBarHtmls & " 下一页"
     Else
      xBarHtmls = xBarHtmls & " 下一页"
     End If
     xBarHtmls = xBarHtmls & " 尾页
Processed in " & lib.ExeTime(starTime) & " second(s)
"
   
  ElseIf xBar=2 Then
   For xI = xPageStart To xPageEnd
    If xI = xPage Then
     xPageTmp = xPageTmp & " " & xI & " "
    Else
     xPageTmp = xPageTmp & " " & xI & " "
    End If
   Next
   xBarHtmls = "" 
   xBarHtmls = xBarHtmls & ""
    xBarHtmls = xBarHtmls & ""
      xBarHtmls = xBarHtmls & ""
     xBarHtmls = xBarHtmls & ""
     xBarHtmls = xBarHtmls & ""
     xBarHtmls = xBarHtmls & ""
      xBarHtmls = xBarHtmls & ""
    xBarHtmls = xBarHtmls & ""
   xBarHtmls = xBarHtmls & "
Pages " & xPage & " / Total " & xPageCount & "  " & xRecordCount & "条记录  首页"
     If xPage>1 Then
      xBarHtmls = xBarHtmls & " 上一页"
     Else
      xBarHtmls = xBarHtmls & " 上一页"
     End If
     xBarHtmls = xBarHtmls & xPageTmp
     If xPage       xBarHtmls = xBarHtmls & " 下一页"
     Else
      xBarHtmls = xBarHtmls & " 下一页"
     End If
     xBarHtmls = xBarHtmls & " 尾页
"
  Else
   xBarHtmls = "" 
   xBarHtmls = xBarHtmls & ""
     xBarHtmls = xBarHtmls & ""
    xBarHtmls = xBarHtmls & ""
     xBarHtmls = xBarHtmls & ""
   xBarHtmls = xBarHtmls & "
当前第 " & xPage & " 页  共 " & xPageCount & "  页    转到"
      xBarHtmls = xBarHtmls & ""
    xBarHtmls = xBarHtmls & "页
"
  End If
  If Isnull(xTmp) Then xBarHtmls = ""
  ShowBar = xBarHtmls
 End Function
 
End Class
%>

 

热门栏目