ASP Efficient Paging program code

Source: Internet
Author: User
Tags mssql


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

'-----------------------------------------------------------------------------------------------
On Error Resume Next
DIM Startime,endtime
' Statistical Execution time
Startime=timer ()
' Connect to the database
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
'-----------------------------------------------------------------------------------------------
%>
<title> Leaf ASP Paging Class-access invoke demo </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<link rel= "stylesheet" href= "Page.css" type= "Text/css" >
<body bgcolor= "#FFFFFF" text= "#000000" >

<table width= "760" border= "1" cellspacing= "0" cellpadding= "4" align= "center" bordercolordark= "#FFFFFF" bordercolorlight= "#CCCCCC" >
&LT;TR align= "center" >
&LT;TD width= ">ID</td>"
&LT;TD width= > Title </td>
&LT;TD width= "*" > Content (Display first 20 words) </td>
&LT;TD width= > Time </td>
</tr>
<%
Dim Ors
Set ors=new cls_showopage ' Create object
With ORS
. Conn=conn ' Get database Connection object
. Dbtype= "AC"
' Database type, AC for access,mssql sqlserver2000,mssql_sp for stored procedure version, MySQL for mysql,pgsql for PostgreSQL
. Rectype=0
' Total number of records method (0 Execution count,1 SQL statement, 2 fixed value)
. Recsql=0
' If rectype=1 = take the SQL statement, if it is 2 = value, equals 0 = ""
. Recterm=0
' Take from the record condition whether has the change (0 unchanged, 1 has the change, 2 does not set the cookie to be timely statistics, applies in the search time)
. Cookiename= "RECAC" ' if recterm=2,cookiesname= ', otherwise write cookiesname
. Order=0 ' Sort (0 Order 1 descending), note the sort corresponding to the primary key ID in the following SQL
. Pagesize=30 ' per page of record bar
. Jsurl= "" ' Showo_page.js Path
. Sql= "id,aaaa,bbbb,cccc$table1$$ $id" field, table, condition (no where), sort (no order by required), primary key ID
End With

Ireccount=ors. RecCount () ' Total records
Irs=ors. ResultSet () ' Return to ResultSet
If ireccount<1 then%>
<tr bgcolor= "" >
<td > No record </td>
</tr>
<%
Else
For i=0 to Ubound (irs,2)
Bgcolor= "#FFFFFF"
If I mod 2=0 then bgcolor= "#DFEFFF"
%>
<tr bgcolor= "<%=bgColor%>" >
&LT;TD width= "><%=irs" (0,i)%></td>
&LT;TD width= "><%=irs" (1,i)%></td>
&LT;TD width= "*" ><%=left (IRs (2,i)%></td>
&LT;TD width= "><%=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>
&LT;TD align= "center" >
<%endtime=timer ()%>
This page execution time: <%=formatnumber ((endtime-startime) *1000,3)%> Ms </td>
</tr>
</table>
</body>
<%
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 use

'================================================================
' Initialization of the Class_Initialize class
'================================================================
Private Sub Class_Initialize
showo_pagesize=10 ' Sets the default value of 10 per page record bar
Showo_currpage=checknum (("Page"), 1,-1) ' Get when the previous value
Showo_order= ">" ' Default sort
Showo_size= "MAX" ' default sort
Showo_whereother= "" ' Default condition
End Sub

'================================================================
' Conn Get database Connection object
'================================================================
Public Property Let Conn (ByVal objconn)
Set Showo_conn=objconn
End Property

'================================================================
' DbType get the database type
'================================================================
Public Property Let DbType (ByVal strdbtype)
Showo_dbtype=strdbtype
End Property

  ' ================================================================
  ' rectype total number of records taken (0 execution count, 1 Write SQL statement take, 2 fixed value
  ' ================================================================
 public Property Let RecType (ByVal intrectype)
  showo_rectype=checknum (intrectype,0,2)
 end Property
 
  ' ================================================================
  ' Recsql ' If rectype=1 = takes a log of the SQL statement, if it is 2 = numeric, equal to 0= ""
  =============================================================== =
 public Property Let Recsql (ByVal strrecsql)
  showo_recsql=strrecsql
 end Property

'================================================================
' Recterm search conditions Change (0 No change, 1 change)
'================================================================
Public Property Let Recterm (ByVal intrecterm)
Showo_recterm=checknum (intrecterm,0,2)
End Property

'================================================================
' CookieName get CookieName
'================================================================
Public Property Let CookieName (ByVal strcookiename)
Showo_cookiename=strcookiename
End Property

'================================================================
' Order sort (0 orders, 1 descending)
'================================================================
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 sets the number of record bars per page, default 10 records
'================================================================
Public Property Let PageSize (ByVal intpagesize)
Showo_pagesize=checknum (intpagesize,showo_pagesize,-1)
End Property

  ' ================================================================
  ' jsurl get showo_page.js path
  ' ================================================================
 public Property Let JsUrl ( ByVal strjsurl)
  showo_jsurl=strjsurl
 end Property

'================================================================
' SQL gets the order of the table fields required for SQL, input: fields, tables, conditions, sort, primary 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 get the total number of records
'================================================================
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 correction record total
'================================================================
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 returns a paginated recordset
'================================================================
Public Property Get ResultSet ()
Resultset=null
' Total records
Showo_reccount=reccount ()
' Current page
If showo_reccount>0 Then
' Pages
If (showo_reccount mod showo_pagesize) =0 Then
Showo_pagecount=showo_reccountshowo_pagesize
Else
Showo_pagecount=showo_reccountshowo_pagesize+1
End If
' Current page
Showo_currpage=checknum (Showo_currpage,1,showo_pagecount)
Select Case Showo_dbtype
Case "AC" ' AC ' database
Set showo_rs=server.createobject ("ADODB. RecordSet ")
Resultset_sql= "SELECT" &Showo_Field& "from" &Showo_Table& "&Showo_Where&" "&showo_ By
Showo_rs.open resultset_sql,showo_conn,1,1,&h0001
showo_rs.absoluteposition= (showo_currpage-1) *showo_pagesize+1
Case "MSSQL" ' sqlserver2000 database
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 database stored procedure version
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 (=showo_order)
Showo_rs.cursorlocation=1
Showo_rs.locktype=1
Showo_rs.open showo_cm
Case Else ' other situations are handled in the most original way
Set showo_rs = Server.CreateObject ("ADODB. RecordSet ")
Resultset_sql= "SELECT" &Showo_Field& "from" &Showo_Table& "&Showo_Where&" "&showo_ By
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

  ' ================================================================
  input: Check field, start number (default number), End number ( 1 does not check size)
  ' ================================================================
 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 class logoff
'================================================================
Private Sub Class_Terminate ()
If IsObject (showo_conn) Then
Showo_conn.close
Set showo_conn=nothing
End If
End Sub

'================================================================
' Up and down pages section
'================================================================
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= ' cellspacing= ' 0 ') cellPadding = ' 0 ' width= ' border= ' 0 ' ><tr><td valign= ' bottom ' style= ' Font-family:verdana, XXFarEastFont-Arial; font-size:11.5px; line-height:15px ' > ', "</td></tr></table>", "page: <font color= ' Red ' >", "</font>/", "" , "&nbsp;", "&nbsp; per page <font color= ' red ' >", "</font>&nbsp;", "&nbsp; total: <font color=" Red ' > ', ' </font></td><td valign= ' bottom ' align= ' right ' style= ' Font-family:verdana, XXFarEastFont-Arial; 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; Jump: "," <font color= ' orange ' >[', "]</font>", "", "", "<font color= ' Red ' >", "</font>", "", "", <%= RecCount ()%>,<%=showo_pagesize%>,2)
</Script>
<%end Sub

End Class%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.