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
%>
Fixed:
<%
If Not Rs_News.eof then
Do while Not Rs_News.eof and RowCount> 0
%>
The public module lacks <form>. After modification:
<%
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
FileName = Mid (fileName, postion)
%>
<Table border = 0 width = '000000'>
<Tr>
<Td width = "258" align = left> total number of pages: <font color = # ff3333> <% = TotalPage %> </font>
Total number of current <font color = # ff3333> <% = PageNo %> </font> pages <% = RecordCount %> </td>
<Td width = "308" align = "right"> <div align = "center">
<% 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 %>
</Div> </td>
<Td width = 189> <form name = "form1" method = "post" action = ""> go to the <% 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 %>