Public paging module in ASP project-application Tips

Source: Internet
Author: User
Tags rowcount servervariables
In large ASP projects, many pages involve page-flipping functions. If each page writes a flip-through program, this kind of work reduces the working efficiency, also is not advantageous to the project modularization, cannot make the code reuse. Therefore, it is necessary to modularize the function of paging.
Design method:
1, call the module, only need to pass the recordset and the number of records displayed on each page;
2, you can click on the link to page, you can also directly enter the page number, return to the page;
3, do not consider the filename, the program every page can be in the current pages.

To figure out the top 3 questions, our public paging module is ready to go.

<%
'+++++++++++++++++++++++++++++++++++++
' Module name: Public paging module
' FileName: turnpage.asp
' Incoming parameters: Rs_tmp (Recordset), PageSize (number of record bars per page)
' Output: Recordset paging display feature
'+++++++++++++++++++++++++++++++++++++
'
Sub turnpage (ByRef rs_tmp,pagesize) ' rs_tmp record set; PageSize the number of record bars displayed per page;
Dim totalpage ' Total pages
Dim PageNo ' is currently showing the first few pages
Dim RecordCount ' Total record bar count
Rs_tmp. PageSize = PageSize
RecordCount = rs_tmp. RecordCount
Totalpage = INT (recordcount/pagesize *-1) *-1
PageNo = Request.QueryString ("PageNo")
' Direct input page jump;
If Request.Form ("PageNo") <> "" Then PageNo = Request.Form ("PageNo")
' If the page is not 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 each page is on the current screen;
Dim filename,postion
FileName = Request.ServerVariables ("Script_name")
postion = InStrRev (FileName, "/") +1
' Get the current file name so that the page-flipping link points to the current file;
FileName = Mid (filename,postion)
%>
<table border=0 width= ' 100% ' >
<tr>
&LT;TD align=left> Total Pages: <font color= #ff3333 ><%=TotalPage%></font> page
Current page <font color= #ff3333 ><%=PageNo%></font> page </td>
&LT;TD align= "Right" >
<%if RecordCount = 0 or totalpage = 1 Then
Response.Write "Home | front page | next page | last"
Else%>
<a href= "&LT;%=FILENAME%&GT;? Pageno=1 "> Home |</a>
<%if PageNo-1 = 0 Then
Response.Write "Previous Page |"
Else%>
<a href= "&LT;%=FILENAME%&GT;? Pageno=<%=pageno-1%> "> front page |</a>
<%end If

If pageno+1 > Totalpage Then
Response.Write "Back Page |"
Else%>
<a href= "&LT;%=FILENAME%&GT;? Pageno=<%=pageno+1%> "> Back page |</a>
<%end if%>

<a href= "&LT;%=FILENAME%&GT;? Pageno=<%=totalpage%> "> Last </a>
<%end if%></td>
&LT;TD width=95> go to page
<%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= Please enter the page number, then return >
<%end if%> Page
</td>
</tr>
</table>
<%end sub%>

Of course, you can turn the link into the page to make the picture button, so that the surface will be more beautiful.

Call Method:
1. Include the paging module file at the beginning of the program or where the page is to be used;
2, define variables: RowCount, the number of records displayed per page
3, call the page process: called Turnpage (Recordset, ROWCOUNT)
4. Add the "RowCount > 0" condition to the condition of the Do While loop output recordset
5, at the end of the loop "loop before" plus: RowCount = RowCount-1

'-----------------------------------------------------
Invocation Example:
FileName: 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 ' captured recordset

' Public paging module starts%>
<!--#include file=. /public/turnpage.asp-->
<%
Dim ROWCOUNT
RowCount = 10 ' Number of record bars displayed per page
Call Turnpage (Rs_news,rowcount)
' Public paging module end%>

<table width=100%>
<tr>
<td> News Number </td>
<td> News title </td>
<td> Release date </td>
<tr>
<%
If not rs_news.eof
Do, 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
%>



Correction:
<%
If not rs_news.eof Then
Do, not rs_news.eof and rowcount>0
%>

and the public module is short of <form&gt, and after the change:
<%
Sub turnpage (ByRef rs_tmp,pagesize) ' rs_tmp record set; PageSize the number of record bars displayed per page;
Dim totalpage ' Total pages
Dim PageNo ' is currently showing the first few pages
Dim RecordCount ' Total record bar count
Rs_tmp. PageSize = PageSize
RecordCount = rs_tmp. RecordCount
Totalpage = INT (recordcount/pagesize *-1) *-1
PageNo = Request.QueryString ("PageNo")
' Direct input page jump;
If Request.Form ("PageNo") <> "" Then PageNo = Request.Form ("PageNo")
' If the page is not 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 each page is on the current screen;
Dim filename,postion
FileName = Request.ServerVariables ("Script_name")
postion = InStrRev (FileName, "/") +1
FileName = Mid (filename,postion)
%>
<table border=0 width= ' 100% ' >
<tr>
&LT;TD width= "258" align=left> Total pages: <font color= #ff3333 ><%=TotalPage%></font> page
Current <font color= #ff3333 ><%=PageNo%></font> page Total <%=RecordCount%> article </td>
&LT;TD width= "308" align= "right" > <div align= "Center" >
<%if RecordCount = 0 or totalpage = 1 Then
Response.Write "Home | front page | next page | last"
Else%>
<a href= "&LT;%=FILENAME%&GT;? Pageno=1 "> Home |</a>
<%if PageNo-1 = 0 Then
Response.Write "Previous Page |"
Else%>
<a href= "&LT;%=FILENAME%&GT;? Pageno=<%=pageno-1%> "> front page |</a>
<%end If

If pageno+1 > Totalpage Then
Response.Write "Back Page |"
Else%>
<a href= "&LT;%=FILENAME%&GT;? Pageno=<%=pageno+1%> "> Back page |</a>
<%end if%>
<a href= "&LT;%=FILENAME%&GT;? Pageno=<%=totalpage%> "> Last </a>
<%end if%>
</div></td>
&LT;TD width=189><form name= "Form1" method= "Post" action= "" > Go to <% If totalpage = 1 then%>
<input type=text name=pageno size=3 readonly disabled style= "background: #d3d3d3" >
<input type= "Submit" name= "submit" value= "Go" disabled style= "background: #d3d3d3" >
<%Else%>
<input type=text Name=pageno size=3 >
<input type= "Submit" name= "submit" value= "Go" >
<%end if%>
</form>
Page
</td>
</tr>
</table>
<%end sub%>
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.