asp高效分頁程式碼

來源:互聯網
上載者:User


<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="Cls_ShowoPage.asp"-->
<%

'-----------------------------------------------------------------------------------------------
On Error Resume Next
DIM startime,endtime
'統計執行時間
startime=timer()
'串連資料庫
DIM Db,Conn,Rs
Db = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("db.mdb")
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.open Db
'-----------------------------------------------------------------------------------------------
%>
<html>
<head>
<title>葉子ASP分頁類-access調用示範</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="page.css" type="text/css">
</head>
<body bgcolor="#FFFFFF" text="#000000">

<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 ors
Set ors=new Cls_ShowoPage '建立對象
With ors
 .Conn=conn   '得到資料庫連接對象
 .DbType="AC"
 '資料庫類型,AC為access,MSSQL為sqlserver2000,MSSQL_SP為預存程序版,MYSQL為mysql,PGSQL為PostGreSql
 .RecType=0
 '取記錄總數方法(0執行count,1自寫sql語句取,2固定值)
 .RecSql=0
 '如果RecType=1則=取記錄sql語句,如果是2=數值,等於0=""
 .RecTerm=0
 '取從屬記錄條件是否有變化(0無變化,1有變化,2不設定cookies也就是及時統計,適用於搜尋時候)
 .CookieName="recac" '如果RecTerm=2,cookiesname="",否則寫cookiesname
 .Order=0   '排序(0順序1降序),注意要和下面sql裡面主鍵ID的排序對應
 .PageSize=30  '每頁記錄條數
 .JsUrl=""   'showo_page.js的路徑
 .Sql="id,aaaa,bbbb,cccc$table1$$$id" '欄位,表,條件(不需要where),排序(不需要需要ORDER BY),主鍵ID
End With

iRecCount=ors.RecCount()'記錄總數
iRs=ors.ResultSet()  '返回ResultSet
If  iRecCount<1 Then%>
<tr bgcolor="">
    <td >暫無記錄</td>   
  </tr>
<%
Else    
    For i=0 To Ubound(iRs,2)
 bgColor="#FFFFFF"
 if i mod 2=0 then bgColor="#DFEFFF"
 %>
  <tr bgcolor="<%=bgColor%>">
    <td width="60"><%=iRs(0,i)%></td>
    <td width="150"><%=iRs(1,i)%></td>
    <td width="*"><%=left(iRs(2,i),20)%></td>
    <td width="150"><%=iRs(3,i)%></td>
  </tr><%
    Next 
End If
%>
</table>
<table width="760" border="0" cellspacing="2" cellpadding="2" align="center">
  <tr>
    <td>
<%ors.ShowPage()%>
</td>
  </tr> 
</table>
<table width="760" border="0" align="center" cellpadding="2" cellspacing="2">
  <tr>
    <td align="center">
      <%endtime=timer()%>
      本頁面執行時間:<%=FormatNumber((endtime-startime)*1000,3)%>毫秒</td>
  </tr>
</table>
</body>
</html>
<%
iRs=NULL
ors=NULL
Set ors=NoThing
%>
<%

Class Cls_ShowoPage
 Private Showo_PageSize,Showo_CurrPage
 Private Showo_Conn,Showo_DbType,Showo_RecType,Showo_RecSql,Showo_RecTerm,Showo_CookieName
 Private S_Order,Showo_JsUrl
 Private Showo_Sql,Showo_Field,Showo_Table,Showo_Where,Showo_OrderBy,Showo_Id
 Private Showo_RecCount,Showo_PageCount,ResultSet_Sql
 Private Showo_Cm,Showo_WhereOther,Showo_Order,Showo_Size,Showo_Mm 'MSSQL用

 '================================================================
 ' Class_Initialize 類的初始化
 '================================================================
 Private Sub Class_Initialize
  Showo_PageSize=10 '設定每頁記錄條數的預設值為10
  Showo_CurrPage=CheckNum(Trim(Request("Page")),1,-1) '擷取當前面的值
  Showo_Order=">" '預設排序
  Showo_Size="MAX" '預設排序
  Showo_WhereOther="" '預設條件
 End Sub

 '================================================================
 ' Conn 得到資料庫連接對象
 '================================================================
 Public Property Let Conn(ByVal objConn)
  Set Showo_Conn=objConn
 End Property

 '================================================================
 ' DbType 得到資料庫類型
 '================================================================
 Public Property Let DbType(ByVal strDbType)
  Showo_DbType=strDbType
 End Property

 '================================================================
 ' RecType 取記錄總數方法(0執行count,1自寫sql語句取,2固定值)
 '================================================================
 Public Property Let RecType(ByVal intRecType)
  Showo_RecType=CheckNum(intRecType,0,2)
 End Property
 
 '================================================================
 ' RecSql '如果RecType=1則=取記錄sql語句,如果是2=數值,等於0=""
 '================================================================
 Public Property Let RecSql(ByVal strRecSql)
  Showo_RecSql=strRecSql
 End Property

 '================================================================
 ' RecTerm 搜尋條件是否變化(0無變化,1有變化)
 '================================================================
 Public Property Let RecTerm(ByVal intRecTerm)
  Showo_RecTerm=CheckNum(intRecTerm,0,2)
 End Property

 '================================================================
 ' CookieName 取得cookiename
 '================================================================
 Public Property Let CookieName(ByVal strCookieName)
  Showo_CookieName=strCookieName
 End Property

 '================================================================
 ' Order 排序(0順序,1降序)
 '================================================================
 Public Property Let Order(ByVal intOrder)
  S_Order=CheckNum(intOrder,0,1)
  If S_Order=1 Then
   Showo_Order="<"
   Showo_Size="MIN"
  End If
 End Property

 '================================================================
 ' PageSize 設定每一頁記錄條數,預設10記錄
 '================================================================
 Public Property Let PageSize(ByVal intPageSize)
  Showo_PageSize=CheckNum(intPageSize,Showo_PageSize,-1)
 End Property

 '================================================================
 ' JsUrl 取得showo_page.js的路徑
 '================================================================
 Public Property Let JsUrl(ByVal strJsUrl)
  Showo_JsUrl=strJsUrl
 End Property

 '================================================================
 ' Sql 取得sql所需表欄位條件排序,輸入:欄位,表,條件,排序,主ID
 '================================================================
 Public Property Let Sql(ByVal str_sql) 
  Showo_Sql=Split(str_sql,"$")
  Showo_Field=Showo_Sql(0)
  Showo_Table=Showo_Sql(1)
  Showo_Where=Showo_Sql(2)
  Showo_OrderBy=Showo_Sql(3)
  Showo_Id=Showo_Sql(4)
  If Len(Showo_Where)>=3 Then
   Showo_WhereOther=" And "&Showo_Where
   Showo_Where=" Where "&Showo_Where
  End If
  If Len(Showo_OrderBy)>3 Then Showo_OrderBy=" ORDER BY "&Showo_OrderBy
 End Property

 '================================================================
 ' GetRecCount 取得記錄總數
 '================================================================
 Private Function GetRecCount()
  Select Case Showo_RecType
   Case 1
    GetRecCount=Showo_Conn.execute(Showo_RecSql,0,1)(0)
   Case 2
    GetRecCount=CheckNum(Showo_RecSql,0,-1)
   Case Else
    GetRecCount=Showo_Conn.execute("SELECT Count("&Showo_Id&") FROM "&Showo_Table&" "&Showo_Where,0,1)(0)
  End Select
 End Function
 
 '================================================================
 ' RecCount 修正記錄總數
 '================================================================
 Public Property Get RecCount()
  RecCount=Request.Cookies("ShowoPage")(Showo_CookieName)
  RecCount=CheckNum(RecCount,0,-1)
  Select Case Showo_RecTerm
   Case 1
    RecCount=GetRecCount()
    Response.Cookies("ShowoPage")(Showo_CookieName)=RecCount
   Case 2
    RecCount=GetRecCount()
   Case Else
    If RecCount=0 Then
     RecCount=GetRecCount()
     Response.Cookies("ShowoPage")(Showo_CookieName)=RecCount
    End If
  End Select
 End Property

 '================================================================
 ' ResultSet 返回分頁後的記錄集
 '================================================================
 Public Property Get ResultSet()
  ResultSet=Null
  '記錄總數
  Showo_RecCount=RecCount()
  '當前頁
  If Showo_RecCount>0 Then
  '頁數
   If (Showo_RecCount mod Showo_PageSize)=0 Then
    Showo_PageCount=Showo_RecCountShowo_PageSize
   Else
    Showo_PageCount=Showo_RecCountShowo_PageSize+1
   End If
   '當前頁
   Showo_CurrPage=CheckNum(Showo_CurrPage,1,Showo_PageCount)
   Select Case Showo_DbType
    Case "AC" 'ac資料庫
     Set Showo_Rs=Server.CreateObject ("adodb.RecordSet")
     ResultSet_Sql="SELECT "&Showo_Field&" FROM "&Showo_Table&" "&Showo_Where&" "&Showo_OrderBy
     Showo_Rs.Open ResultSet_Sql,Showo_Conn,1,1,&H0001
     Showo_Rs.AbsolutePosition=(Showo_CurrPage-1)*Showo_PageSize+1
    Case "MSSQL" 'sqlserver2000資料庫
     If Showo_CurrPage=1 Then
      ResultSet_Sql="SELECT TOP "&Showo_PageSize&" "&Showo_Field&" FROM "&Showo_Table&Showo_Where&" "&Showo_OrderBy
     Else
      ResultSet_Sql="SELECT "&Showo_Size&"("&Showo_Id&") FROM (SELECT TOP "&(Showo_CurrPage-1)*Showo_PageSize&" "&Showo_Id&" FROM "&Showo_Table&Showo_Where&" "&Showo_OrderBy&") AS tmpTable"
      Showo_Mm=Showo_Conn.execute(ResultSet_Sql,0,1)(0)
      ResultSet_Sql="SELECT TOP "&Showo_PageSize&" "&Showo_Field&" FROM "&Showo_Table&" WHERE "&Showo_Id&Showo_Order&Showo_Mm&Showo_WhereOther&" "&Showo_OrderBy
     End If
     Set Showo_Rs=Showo_Conn.execute(ResultSet_Sql)
    Case "MSSQL_SP" 'sqlserver2000資料庫預存程序版
     Set Showo_Rs=server.CreateObject("Adodb.RecordSet")
     Set Showo_Cm=Server.CreateObject("Adodb.Command")
     Showo_Cm.CommandType=4
     Showo_Cm.ActiveConnection=Showo_Conn
     Showo_Cm.CommandText="SP_ShowoPage"
     Showo_Cm.parameters(1)=Showo_CurrPage
     Showo_Cm.parameters(2)=Showo_PageSize
     Showo_Cm.parameters(3)=Showo_Field
     Showo_Cm.parameters(4)=Showo_Table
     Showo_Cm.parameters(5)=Showo_Where
     Showo_Cm.parameters(6)=Showo_WhereOther
     Showo_Cm.parameters(7)=Showo_OrderBy
     Showo_Cm.parameters(8)=Showo_Id
     Showo_Cm.parameters(9)=Showo_Size
     Showo_Cm.parameters(10)=Showo_Order
     Showo_Rs.CursorLocation=1
     Showo_Rs.LockType=1
     Showo_Rs.Open Showo_Cm
    Case Else '其他情況按最原始的方法處理
     Set Showo_Rs = Server.CreateObject ("adodb.RecordSet")
     ResultSet_Sql="SELECT "&Showo_Field&" FROM "&Showo_Table&" "&Showo_Where&" "&Showo_OrderBy
     Showo_Rs.Open ResultSet_Sql,Showo_Conn,1,1,&H0001
     Showo_Rs.AbsolutePosition=(Showo_CurrPage-1)*Showo_PageSize+1
   End Select
   ResultSet=Showo_Rs.GetRows(Showo_PageSize)
   Showo_Rs.close
   Set Showo_Rs=Nothing
  End If 
 End Property

 '================================================================
 ' 輸入:檢查欄位,開始數字(預設數字),結束數字(為-1則不檢查大小)
 '================================================================
 Private Function CheckNum(ByVal strStr,ByVal intStartNum,ByVal intEndNum)
  CheckNum=intStartNum
  If IsNumeric(strStr) Then CheckNum=Clng(strStr)       
  If intEndNum>-1Then
   If CheckNum<intStartNum Then CheckNum=intStartNum
   If CheckNum>intEndNum Then CheckNum=intEndNum
  End If
 End Function

 '================================================================
 ' Class_Terminate 類登出
 '================================================================
 Private Sub Class_Terminate()
  If IsObject(Showo_Conn) Then
   Showo_Conn.Close
   Set Showo_Conn=Nothing
  End If
 End Sub

 '================================================================
 ' 上下頁部分
 '================================================================
 Public Sub ShowPage()%>
  <Script Language="JavaScript" type="text/JavaScript" src="<%=Showo_JsUrl%>showo_page.js"></Script>
  <Script Language="JavaScript">
  ShowoPage("<table style='BORDER-COLLAPSE: collapse' borderColor='#111111' height='10' cellSpacing='0' cellPadding='0' width='95%' border='0'><tr><td vAlign='bottom'  style='font-family: Verdana,宋體; font-size: 11.5px; line-height: 15px'>","</td></tr></table>","頁次:<font color='red'>","</font>/","","&nbsp;","&nbsp;每頁<font color='red'>","</font>&nbsp;","&nbsp;共計:<font color='red'>","</font></td><td vAlign='bottom' align='right'  style='font-family: Verdana,宋體; font-size: 11.5px; line-height: 15px'>","<font face=webdings>9</font>","<font face=webdings>7</font>","<font face=webdings>8</font>","<font face=webdings>:</font>","&nbsp;&nbsp;跳轉:","<font color='orange'>[","]</font>","","","<font color='red'>","</font>","","",<%=RecCount()%>,<%=Showo_PageSize%>,2)
  </Script>
 <%End Sub

End Class%>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.