If the repost is convenient, the program is used, as long as you can go online, you can copy. Create an ASP universal paging class
Author: Smoothing Time: 2004-6-30 Document Type: Reprinted from: unintentional Liu
From the beginning of learning to use ASP, there are still many programs, the most headache is written data paging, each time due to several variable names or several parameters, which requires which paragraph every time The lengthy and complicated paging code, the code is long, which makes the program's readability, easy to travel, the debug is not missing for a long time, so slowly I start using some online supplied paging functions or panesses. It is indeed a lot of things, but the usual functions and classes are also encapsulated on the data display section. Every time I want to change the function or class itself, it is not convenient to use it. I have been complicated enough to write the paging it yourself. Don't say others.
So, I have time I wrote a paging class yesterday, I feel good, I don't want to use eggs. I share my experience with you (talk about experience, thinking about it). Here I don't want to say the principle of paging, anyway, everyone knows, I have to talk about it in depth, I will not. Ha ha.
First, the goal of creating a page class before writing, I thought, what kind of class I want to write, I think about the previous write process, the most annoying, which is the complex pagination code for each time you have to write. The biggest trouble is only a few variable names every time. So the first thing to implement is to put this package, the second is to encapsulate the navigation bar of the paging, the third, not used to encapsulate the data display parts, which is not convenient to program, For those who have different display effects every time, it is more troublesome than yourself. So my purpose is some simple packages for RecordSet.
Second, the creation process so the first attribute I wrote is to return a processed Recordse.
Public Property Get GetRs () Set XD_Rs = Server.createobject ( "adodb.recordset") XD_Rs.PageSize = PageSize XD_Rs.Open XD_SQL, XD_Conn, 1,1 If not (XD_Rs.eof and XD_RS.BOF) Then If int_curpage> XD_RS .Pagecount kilpagecount = xd_rs.pagecount end = int_curpage end if set getrs = xd_rsend property
The role of this attribute is more than the current surface of the specified Recordset and the first record of the pointer points to the current page. This should be the core of the entire class, of course, some of the parameters are obtained by other properties. So here is the basic parameters of this class to be introduced here.
====================================================== 'getConn gets database connection '' ============================================= Public property ==== Public Property Let GetConn (OBJ_CONN) SET XD_CONN = Obj_Connend Property '=========================================== ===== 'getSQL get query statement' '======================================== ======== Public property let getsql (str_sql) XD_SQL = STR_SQLEND PROPERTY
'===============================================' Pagesize Attribute 'Sets the page size of each page' ======================================== ======= Public Property Let PageSize (int_PageSize) If IsNumeric (int_Pagesize) Then XD_PageSize = CLng (int_PageSize) Else str_error = str_error & "PageSize parameter is incorrect" ShowError () End IfEnd PropertyPublic Property Get PageSize If XD_PageSize = "" or (not (isnumeric (xd_pagesize)) later pagesize = xd_pagesize end ifend property
The above is the need to specify the parameters in the process of using the class. I used to judge every incoming parameter when writing attributes. It is the robust of the class, but I wanted to go later. This is not necessary to talk to the ASP, it is not enough to speed up the point speed. As for why, I think you will find it during use, it is better not to add. This is also after I have been thinking, I only retain some necessary verification. A parameter is the current page. In the program I use int_curpage to identify, this is not good during the creation of the class.
'========================' Set some parameters '黙 bad value' ================ ========= xd_pagesize = 10 'Set the default value of the paging is 10' ======================== 'Get the front Value '================================= "" "" "" = "" "))))")) <1 THEN INT_CURPAGE = 1 else int_curpage = 1 else int_curpage = cint (TRIM (Request ("page")) End if End Sub here The function of the division is basically realized, as long as the URL of the page is called, it will display the contents of the nth page, so the next thing to do is to create a data navigation bar, I Designed it in the form of surface 9 3 [1] [2] [3] [4] [5] [6] [7] [8] 4: Page: 1/8 page Total 51 records 7 / Per page
In the page, the showpage can be called in any location after GETRS, or call multiple times.
Public Sub ShowPage () Dim str_tmpint_totalRecord = XD_RS.RecordCountIf int_totalRecord <= 0 Then str_error = str_error & "the total number of records is zero, the data enter" Call ShowError () End IfIf int_totalRecord = "" Then int_TotalPage = 1Else If int_totalRecord mod PageSize = 01inint_totalpage = clng (int_totalRecord / xd_pagesize * -1) * - 1 elseint_totalpage = clng (int_totalRecord / xd_pagesize * -1) * - 1 1 End IFEND IF
IF INT_CURPAGE> INT_TOTALPAGE THEN INT_CURPAGE = INT_TOTALPAGEEND IF
'==================================================== ==== 'Display paging information, each module changes the location of the expansion "=====================================================================★ ====================== Response.write "str_tmp = showfirstprv" Show home, previous page response.write str_tmp str_tmp = shownumbtn 'digital navigation response.write str_tmpstr_tmp = ShowNextLast 'Next page, the last page response.write str_tmpstr_tmp = showpageinforesponse.write str_tmpresponse.write "" "" "End Sub to this class" is complete (in order to save the layout, I have no way to put it up, then there is all the complete Code) Write a simple page test
<% 'Puts the panelock class into set conn = server.createObject ("adoDb.connection" conn.open "driver = {Microsoft Access Driver (* .mdb)}; dbq =" & Server.mappath ("pages.mdb ")
'################################################################################################## = conn'sql statement mypage.getsql = "select * from [test] ORDER BY ID ASC" "Sets each page record strip data to 5 mypage.pageSize = 5 'Return RecordsetSet RS = mypage.getrs ()' display Paging information, this method can, after SET RS = mypage.getrs (), you can call multiple times.showpage.ShowPage.GeTRS ()
'Display data response.write ("<") for i = 1 to mypage.pageSize' This can be customized in the display method if not rs.eof kilite rs (0) & " "Rs.movenext else exit for end ifnext%>
The effect is not bad, there should be all.
During the paging process, there is a problem with the problem with the problem that in the URL with multiple parameters, if it does not drop the other parameters when the page is turned. I rely on a getURL process and call when generating navigation. Private Function GetURL () Dim strurl, str_url, i, j, search_str, result_url search_str = "page =" strurl = Request.ServerVariables ( "URL") Strurl = split (strurl, "/") i = UBound (strurl, 1 ) str_url = strurl (i) 'get the current page file name str_params = Request.ServerVariables ( "QUERY_STRING") If str_params = "" Then result_url = str_url & "? page =" Else If InstrRev (str_params, search_str) = 0 Thenresult_url = str_url & "?" & str_params & "& page =" Elsej = InstrRev (str_params, search_str) -2If j = -1 Then result_url = str_url & "? page =" Else str_params = Left (str_params, j) result_url = str_url & " ? "& str_params &" & page = "end if end if end if getURL = result_urlend function
With the process of getURL, you can automatically get the front file name, and all tape parameters, implement the page conversion page does not lose parameters. Third, the postscript, the paging class, solve the page partial code that needs to be repeated each time the paging is, which is convenient for programming, which has improved the readability of the main code. I also hope that I can give you a point in the programming process. Because I have limited level, the procedures and articles are wrong, I hope everyone criticizes.
All code download
Attached code: <%
'==================================================== ==================
'Xdownpage ASP version
'Version 1.00
'Code by zykj2000
'Email: zykj_2000@163.net
'Bbs: http://bbs.513soft.net
'This program can be used free, modify, I hope my program can bring your work to make your work.
'But please keep the above call
'
'Program characteristics
'This program is mainly encapsulated by the part of the data paging, and the data display is completely customized by the user,' Supports multiple parameters of the URL
'
'Instructions for use
'Program parameter description
'PapGesize Definition Subject Number of Records of each page
'Getrs returns this property read-only
'GetConn gets database connections
'GetSQL gets query statements
'Program method description
'Showpage display paging navigation bar, unique common method
'
'==================================================== ==================
Const btn_first = " 9 font> "Defines the first page button display style
Const btn_prev = " 3 font> "Define Previous Page button Display Style
Const btn_next = " 4 font> "Defines the next button display style
Const btn_last = ": font> "Defines the last page button display style
Const xd_align = "center" "Defines the page information alignment
Const xd_width = "100%" defines the size of the page information box
Class xdownPage
Private xd_pagecount, xd_conn, xd_rs, xd_sql, xd_pagesize, str_errors, int_curpage, str_URL, int_totalpage, int_totalrecord, xd_surl
'==================================================== ================
'PageSize properties
'Set the page size of each page
'==================================================== ================ PUBLIC PROPERTY Let Pagesize (int_pageSize)
IF isnumeric (int_pagesize) THEN
XD_PageSize = ClNG (int_pagesize)
Else
Str_ERROR = STR_ERROR & "Pagesize is incorrect"
Showerror ()
END IF
End Property
Public property Get Pagesize
IF xd_pagesize = "" or (not (isnumeric (xd_pagesize)).
PageSize = 10
Else
PageSize = xd_pagesize
END IF
End Property
'==================================================== ================
'Getrs attribute
'Returning the record set after paging
'==================================================== ================
Public property Get Get GETRS ()
SET XD_RS = Server.createObject ("AdoDb.Recordset")
XD_RS.PAGESIZE = PageSize
XD_RS.Open XD_SQL, XD_CONN, 1, 1
IF not (xd_rs.eof and xd_rs.bof) THEN
IF INT_CURPAGE> XD_RS.PAGECUNT THEN
INT_CURPAGE = XD_RS.PAGECOUNT
END IF
XD_rs.absolute = int_curbage
END IF
Set getrs = XD_RS
End proty '================================================ ==================
'GetConn gets database connections
'
'==================================================== ================
Public property let getConn (Obj_conn)
SET XD_CONN = Obj_conn
End Property
'==================================================== ================
'GetSQL gets query statements
'
'==================================================== ================
Public property let getsql (STR_SQL)
XD_SQL = STR_SQL
End Property
'==================================================== ================== Initialization of the Class_Initialize class
'Initialize the current page
'
'==================================================== ==================
Private sub coplass_initialize
'==========================
'Set some parameters 黙 recognition
'==========================
XD_PageSize = 10 'Setting the default value of 10
'==========================
'Get the front value
'==========================
If Request ("Page") = "" "
INT_CURPAGE = 1
Elseif Not (ISNUMERIC (Request ("Page")))
INT_CURPAGE = 1
Elseif Cint (TRIM (Request ("Page"))) <1 THEN
INT_CURPAGE = 1
Else
INT_CURPAGE = CINT (TRIM (Request ("Page"))))
END IF
End Sub
'==================================================== ==================== ShowPage creates a paging navigation bar
'There are home, previous page, next page, last page, and digital navigation
'
'==================================================== ===================
Public Sub showpage ()
DIM STR_TMP
XD_SURL = getURL ()
INT_TOTALRECORD = XD_RS.Recordcount
IF int_totalRecord <= 0 THEN
STR_ERROR = STR_ERROR & "The total number of records is zero, please enter the data"
Call showerror ()
END IF
If Int_totalRecord = "" ""
INT_TOTALPAGE = 1
Else
IF INT_TOTALRECORD MOD PAGESIZE = 0 THEN
INT_TOTALPAGE = CLNG (int_totalRecord / xd_pagesize * -1) * - 1
Else
INT_TOTALPAGE = CLNG (int_totalRecord / xd_pagesize * -1) * - 1 1
END IF
END IF
IF INT_CURPAGE> INT_TOTALPAGE THEN
INT_CURPAGE = INT_TOTALPAGE
END IF
'==================================================== ==================
'Display paging information, each module changes the location according to yourself
'==================================================== ================= Response.write "" "
STR_TMP = ShowFirstPRV
Response.write str_TMP
STR_TMP = Shownumbtn
Response.write str_TMP
STR_TMP = Shownextlast
Response.write str_TMP
STR_TMP = showpageInfo
Response.write str_TMP
Response.write ""
End Sub
'==================================================== ===================
'ShowFirstPRV Show Home, Previous Page
'
'
'==================================================== ===================
Private function showfirstprv ()
DIM STR_TMP, INT_PRVPAGE
IF INT_CURPAGE = 1 THEN
STR_TMP = BTN_First & "& btn_prev
Else
INT_PRVPAGE = INT_CURPAGE-1
Str_tmp = "" & btn_first & "" & btn_prev & "" end if
ShowfirstPRV = STR_TMP
END FUNCTION
'==================================================== ===================
'ShowNextlast Next, Last Page
'
'
'==================================================== ===================
Private function shownextlast ()
DIM STR_TMP, INT_NEXTPAGE
IF INT_CURPAGE> = INT_TOTALPAGE THEN
STR_TMP = BTN_NEXT & "& BTN_LAST
Else
INT_NEXTPAGE = INT_CURPAGE 1
Str_TMP = "<" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" " & Btn_last & ""
END IF
Shownextlast = STR_TMP
END FUNCTION
'==================================================== =================== 'shownumbtn digit navigation
'
'
'==================================================== ===================
Private function shownumbtn ()
DIM I, STR_TMP
For i = 1 to int_totalpage
Str_TMP = STR_TMP & "[" & I & "]"
NEXT
Shownumbtn = str_tmp
END FUNCTION
'==================================================== ===================
'ShowPageInfo Pipe Information
'It is more important to modify itself.
'
'==================================================== ===================
Private function showpageinfo () DIM STR_TMP
Str_tmp = "Page:" & INT_CURPAGE & "Page Total" & INT_TOTALRECORD & "Summary Record" & XD_PageSize & "Bar / Each page"
ShowpageInfo = STR_TMP
END FUNCTION
'==================================================== ==================
'GetURL gets the current URL
'Different by the URL parameters, get different results
'
'==================================================== ==================
Private function getURL ()
DIM STRURL, STR_URL, I, J, Search_Str, Result_URL
Search_str = "Page ="
Strurl = Request.serverVariables ("URL")
Strurl = Split (Strurl, "/")
I = Ubound (Strurl, 1)
Str_url = Strurl (i) 'Get the current page file name
Str_Params = Trim (Request.ServerVariables ("Query_String"))
If str_params = "" "" "
Result_url = STR_URL & "? Page ="
Else
IF INSTRREV (STR_PARAMS, Search_Str) = 0 THEN
Result_url = STR_URL & "?" & str_params & "& Page ="
Else
J = Instrrev (Str_Params, Search_STR) -2
IF j = -1 Then
Result_url = STR_URL & "? Page ="
Else
Str_Params = Left (str_params, j)
Result_url = STR_URL & "?" & str_params & "& points" end if
END IF
END IF
GetURL = Result_URL
END FUNCTION
'==================================================== ===================
'Set the Terminate event.
'
'==================================================== ===================
Private sub coplass_terminate
XD_rs.close
SET XD_RS = Nothing
End Sub
'==================================================== ===================
'ShowError error prompt
'
'
'==================================================== ===================
Private Sub showerror ()
IF str_error <> "" ""
Response.Write ("" & str_error & ") Response.end
END IF
End Sub
END CLASS
Set conn = server.createObject ("adoDb.connection")
Conn.open "Driver = {Microsoft Access Driver (* .mdb)}; dbq =" & Server.mappath ("pages.mdb")
'############# 类 调 样 样 ######################
'Creating an object
Set mypage = new xdownpage
'Get database connections
mypage.getconn = conn
'SQL statement
mypage.getsql = "SELECT * from" ORDER BY ID ASC "
'Setting the record strip data for each page is 5
mypage.pagesize = 5
'Return to Recordset
SET RS = mypage.getrs ()
'Display paging information, this method can be, after SET RS = mypage.getrs (), can be called in any position, you can call multiple times
mypage.showpage ()
'Display Data
Response.write ("<")
For i = 1 to mypage.pageSize
'Here you can customize the display mode.
IF not r.
Response.write RS (0) & "<"
rs.movenext
Else
EXIT for
END IF
NEXT
%>