ASP Public Paging code

Source: Internet
Author: User
Public Paging module in ASP project
In large ASP projects, many pages involve the paging function. If every page is written with a flip-page program, this work reduces the work efficiency, is not conducive to the modularization of the project, and cannot reuse the code. Therefore, it is necessary to modularize the functions such as page flip.

Design method:
1. When calling this module, you only need to pass the record set and the number of records displayed on each page;
2. You can click the link to flip the page or enter the page number. Press enter to flip the page;
3. Do not consider the file name. The current page can be accessed every time the program goes through pages.

After thinking about the above three questions, we can start the Public Paging module.

<%
'++
◆ Module name: Public Paging Module
◆ File name: turnpage. asp
◆ Input parameter: rs_tmp (record set), pagesize (number of records displayed per page)
◆ Output: record set page flip display
'++
'
Sub turnpage (byref rs_tmp, pagesize) 'rs _ TMP record set; pagesize the number of records displayed on each page;
Total dim totalpage pages
Dim pageno' current page number
Dim recordcount 'Total number of records
Rs_tmp.pagesize = pagesize
Recordcount = rs_tmp.recordcount
Totalpage = int (recordcount/pagesize *-1) *-1
Pageno = request. querystring ("pageno ")
'Directly enter the page number to jump;
If request. Form ("pageno") <> "" Then pageno = request. Form ("pageno ")
'If no page is selected, the first page is displayed by default;
If pageno = "" Then pageno = 1
If recordcount <> 0 then
Rs_tmp.absolutepage = pageno
End if

'Get the current file name so that every page turning is performed on the current page;
Dim filename, postion
Filename = request. servervariables ("script_name ")
Postion = Limit Rev (filename, "/") + 1
'Get the name of the current file and direct the page turning link to the current file;
Filename = mid (filename, postion)
%>
<Table border = 0 width = '000000'>
<Tr>
<TD align = left> total number of pages: <font color = # ff3333> <% = totalpage %> </font>
Current <font color = # ff3333> <% = pageno %> </font> page </TD>
<TD align = "right">
<% If recordcount = 0 or totalpage = 1 then
Response. Write "homepage | front page | back page | last page"
Else %>
<A href = "<% = filename %>? Pageno = 1 "> homepage | </a>
<% If pageno-1 = 0 then
Response. Write "Previous Page |"
Else %>
<A href = "<% = filename %>? Pageno = <% = PageNo-1 %> "> Previous Page | </a>
<% End if

If pageno + 1> totalpage then
Response. Write "Back Page |"
Else %>
<A href = "<% = filename %>? Pageno = <% = pageno + 1%> "> next page | </a>
<% End if %>

<A href = "<% = filename %>? Pageno = <% = totalpage %> "> last page </a>
<% End if %> </TD>
<TD width = 95> go to
<% If totalpage = 1 then %>
<Input type = text name = pageno size = 3 readonly disabled style = "Background: # d3d3d3">
<% Else %>
<Input type = text name = pageno size = 3 value = "" Title = enter the page number and press enter>
<% End if %> page
</TD>
</Tr>
</Table>
<% End sub %>

Of course, you can turn the link on the page into an image button, which will make the page more beautiful.

Call method:
1. Include the paging module file at the beginning of the program or where pages are to be used;
2. Define the variable rowcount, the number of records displayed on each page
3. Call the page turning process: Call turnpage (record set, rowcount)
4. Add the "rowcount> 0" condition to the condition of the do while loop output record set.
5. Add rowcount = rowcount-1 before the loop ends.

'-----------------------------------------------------
Call example:
File Name: News. asp

<%
Dim Conn, rs_news
Set conn = server. Createobject ("ADODB. Connection ")
Conn. Open "CPM", "CPM", "CPM"

Dim SQL
SQL = "select * from news"
Set rs_news = server. Createobject ("ADODB. recordset ")
Rs_news.open SQL, Conn, 1, 3'

'Public paging module start %>
<! -- # Include file = ../public/turnpage. asp -->
<%
Dim rowcount
Rowcount = 10' number of records per page
Call turnpage (rs_news, rowcount)
'Public paging module ended %>

<Table width = 100%>
<Tr>
<TD> News No. </TD>
<TD> News Title </TD>
<TD> release date </TD>
<Tr>
<%
If not rs_news.eof
Do while not rs_news.eof and rowcount> 0
%>
<Tr>
<TD> <% = rs_news ("ID") %> </TD>
<TD> <% = rs_news ("name") %> </TD>
<TD> <% = rs_news ("date") %> </TD>
<Tr>
<%
Rowcount = rowcount-1
Rs_news.movenext
Loop
End if
%>

<% Sub list_page (totalrec, pagesize)
'Totaltec: Total number of records
'Pagesize: How many records are displayed per page
Totalpage = fix (TotalRec-1)/pagesize) + 1' get the total number of pages
Currentpage = request ("page") 'to get the current page
If currentpage = "" or not isinteger (currentpage) then
Currentpage = 1
Else
Currentpage = clng (currentpage)
End if
P = (CurrentPage-1)/10
Response. write "<form method = get> page times: <B>" & currentpage & "</B>/<B>" & totalpage & "</B> page & nbsp; & nbsp; <B> "& pagesize &" </B> total records per page: <B> "& totalrec &" </B> </TD> "&_
"<TD valign = middle> <Div align = right> page :"
If currentpage = 1 then
Response. Write "<font face = webdings color = Red> 9 </font>"
Else
Response. Write "<a href = '? Page = 1 "'title = Home> <font face = webdings> 9 </font> </a>"
End if
If p * 10> 0 then response. Write "<a href = '? Page = "& CSTR (p * 10) &" 'title = previous 10 pages> <font face = webdings> 7 </font> </a>"
Response. Write "<B>"
For II = p * 10 + 1 to p * 10 + 10
If II = currentpage then
Response. Write "<font color = Red>" + CSTR (ii) + "</font>"
Else
Response. Write "<a href = '? Page = "& CSTR (ii) &" '> "+ CSTR (ii) +" </a>"
End if
If II = totalpage then exit
Next
Response. Write "</B>"
If II <n then response. Write "<a href = '? Page = "& CSTR (ii) &" 'title = next 10 pages> <font face = webdings> 8 </font> </a>"
If currentpage = totalpage then
Response. Write "<font face = webdings color = Red >:</font>"
Else
Response. Write "<a href = '? Page = "& CSTR (totalpage) &" 'title = last page> <font face = webdings >:</font> </a>"
End if
Response. write ": <input type = text name = page size = 3 maxlength = 10 value = "& currentpage &"> <input type = button value = go name = submit> </form>"
End sub %>

'Asp generic paging functions Author: Heaven and Earth boy 2005.7.7 twt326@163.com
'Enter the query statement, database name, database connection, current page number, link string, number of list items, and query condition (blank by default)
'Display the navigation link on the top and bottom pages. The page number is displayed in the drop-down box.
'The function is directly changed according to the general paging function 1 of PHP in Heaven and Earth. It uses the SQL efficient paging solution TECHNOLOGY OF A prawns on the csdn BBS.
':) The post is not found here, so don't be surprised. It is more convenient to use with the following functions.
Sub getnav (dbname, Conn, tnowpage, Link, pagelistnum, wherewords)
Dim SQL, trst, sumrows, pagesum, tmphead, showdown, selectcode, I
SQL = "select count (*) as recnum from" & dbname
If wherewords <> "" Then SQL = SQL & "where" & wherewords
Set trst = server. Createobject ("ADODB. recordset ")
Trst. Open SQL, Conn, 1, 1
Sumrows = trst ("recnum ")
Pagesum = sumrows \ pagelistnum + 1
If sumrows mod pagelistnum = 0 then pagesum = sumrows/pagelistnum
Tmphead = "<a href =" & link & "& page ="
If CINT (tnowpage) <2 then
Showdown = "<Table> <form name = 'selform' method = 'post' action =''> <tr> <TD> homepage & nbsp; & nbsp ;"
Else
Showdown = "<Table> <form name = 'selform' method = 'post' action =''> <tr> <TD> "& tmphead &" 1> homepage </> & nbsp; & nbsp; "& tmphead & (tnowpage-1) &"> previous page </a> & nbsp ;"
End if
If CINT (tnowpage) <CINT (pagesum) then
Showdown = showdown & tmphead & (tnowpage + 1) & "> next page </a> & nbsp; "& tmphead & pagesum &"> last page </a> & nbsp ;"
Else
Showdown = showdown & "next page & nbsp; last page & nbsp ;"
End if
'Get the drop-down box and turn to the code
Selectcode = "<script language = 'javascript '> function gopagenav (PAGE) {location ='" & link & "& page = '+ page ;} </SCRIPT> <select name = 'selpage' id = 'selpage' onchange = 'javascript: gopagenav (this. value); '>"
For I = 1 to pagesum
Selectcode = selectcode & "<option value = '" & I &"'"
If CINT (I) = CINT (tnowpage) Then selectcode = selectcode & "selected"
Selectcode = selectcode & "> =" & I & "= </option>"
Next
Selectcode = selectcode & "</SELECT>"
Showdown = showdown & "No. </TD> <TD>" & selectcode & "</TD> <TD> page," & pagesum & "Page & nbsp; total number of records: "& sumrows &" </TD> </tr> </form> </table>"
Response. Write showdown
Trst. Close
Set trst = nothing
End sub
'The query statement is generated based on the parameter to query a certain page of data
'Parameter: number of entries displayed on the current page, data table names (separated by commas), field names to be selected (separated by commas), and query conditions (null strings if unconditional ), id Primary Key, sorting method (ASC positive or DESC reverse)
'Example
'SQL = getfysql (page, pagelistnum, "tbl_message, tbl_login", "tbl_message. *, tbl_login.l_user", "identifier = tbl_login.l_id", "m_id", "DESC ")
'Getnav "tbl_message", Conn, page, "adminmessage. asp? Act = ", pagelistnum, addnavwords
Function getfysql (tnowpage, tpagelistnum, tblname, selfields, wherewords, keyfield, sorttype)
Dim FH, HS
If wherewords = "" Then wherewords = "1 = 1"
FH = ">": HS = "Max"
If sorttype = "DESC" then
FH = "<"
HS = "min"
End if
If (tnowpage-1) * tpagelistnum = 0 then
SQL = "select top" & tpagelistnum & "" & selfields & "from" & tblname & "where" & wherewords & "order by" & keyfield & "& sorttype
Else
SQL = "select top" & tpagelistnum & "" & selfields & "from" & tblname & "where" & wherewords & "and" & keyfield & FH & "(select" & HS & "(" & keyfield &") from (select top "& (tnowpage-1) * tpagelistnum) & "" & keyfield & "from" & tblname & "where" & wherewords & "order by" & keyfield & "" & sorttype & ") as t) order by "& keyfield &" & sorttype
End if
Getfysql = SQL
End Function

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.