Create an ASP pagination class (1) section of the article

zhaozj2021-02-16  44

Create an ASP universal paging class

Wave

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, create the goal of the page class

Before writing, I thought about how to write a class. When I recall, when I have written the paging process, I am most annoying that I have to write which complex paging code is written every time. The biggest trouble every time. It is only a few variable names. 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, create a process

So the first attribute I wrote is to return a processed Recordset

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 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 connections

'

'============================================= ======= plic printy 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

'===============================================

'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

Elsepagesize = xd_pagesize

END IF

End 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.

There is also a parameter is the current page acquisition. In the program I use int_curpage to identify, this is not good in the creation process of the class.

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

The functionality of this class is basically all, as long as the URL of the page is called, add Page = n, it will display the contents of the nth page, so the next thing to do is to create a data navigation. The bar, I design it as a similar 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_tmp int_totalRecord = XD_RS.RecordCount If int_totalRecord <= 0 Then str_error = str_error & "the total number of records is zero, the data enter" Call ShowError () End If If int_totalRecord int_Totalpage Then int_curpage = int_TotalPage End If '==================================================== ==== 'Display paging information, each module changes the location of the expansion "=====================================================================★ ====================== Response.write "

STR_TMP = ShowFirstPRV 'Show Home, Previous Response.write Str_TMP

STR_TMP = shownumbtn 'Digital Navigation

Response.write str_tmp str_tmp = shownextlast 'Next, last page Response.write str_tmp str_tmp = showpageInfo response.write str_tmp response.write " " End Sub

The function here is complete (in order to save the layout, I have not put it up, then there is a complete code) Write a simple page test.

'puts the paging class into in

<%

Set mypage = new xdownpage

mypage.getconn = conn

mypage.getsql = "Select * from productinfo Order by id ASC"

mypage.pagesize = 7

SET RS = mypage.getrs () 'Get Recordset

mypage.showpage () 'Display Page Navigation Bar

For i = 1 to mypage.pagesize 'The following uses and use the general Recordset no two

If not rs.eof kiln 'is used to determine whether to reach the last one, must be added, otherwise the last page is separated

Response.write RS ("Key") & "
" here can display code according to our own meaning,

rs.movenext

Else

EXIT for

END IF

NEXT

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 = "" "" "

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 & "& Page ="

END IF

END IF

END IF

GetURL = Result_URL

END 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, post

Through this paging class, the paging partial code that needs to be repeated each time the paging class is solved, which makes it easy to program, and improve 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. Code download: http://www.513soft.net:83/net/page.rar

转载请注明原文地址:https://www.9cbs.com/read-25834.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.048, SQL: 9