There are still many followers on some forums about paging ASP programs, but there are only codes and no detailed explanations. For beginners, this will always fail to truly grasp it. This time I will explain the paging technology in detail. , let everyone understand ASP paging! First, let’s see the effect!
Take a look at the function: The paging program first reads the number of preset records on each page, here it is 5, and the others will be displayed on the next page. At the same time, it prompts the current page number, total page number, and total record number. When the number of pages is displayed When it is the first page, the links to the home page and the previous page are invalid. When the number of pages displayed is the last page, the links to the next page and the last page are invalid.
Next, I will tell you how to create this paging effect step by step with examples.
First, the field record_info in the database exists in the info table (you may have a certain understanding of the database when learning ASP paging). First, connect to the database and open a record set. The following code:
The following is the quoted content:
<%
Set conn=Server.CreateObject(Adodb.Connection)
connstr=provider=Microsoft.JET.OLEDB.4.0;Data Source=&Server.MapPath(data.mdb)
conn.open connstr
Set rs=Server.CreateObject(Adodb.Recordset)
sql=Select * from info
rs.open sql,conn,1,1
%>
This code is not explained in detail. I believe that beginners will understand it. For a detailed explanation, you can read the tutorial "Teaching you how to use ASP to make a guestbook". Next, this is the more important part of the paging. It only contains three lines:
The following is the quoted content:
<%
rs.pagesize=5
curpage=Request.QueryString(curpage)
rs.absolutepage=curpage
%>
Second sentence:
rs.pagesize =5, what does this mean? It is a built-in property in the Recordset object. Its function is to specify the number of records per page. When set to 5, every 5 records are put together into one page. For example, there are 21 records in total in the instance. Then, use rs After paging by .pagesize, these 21 records will be divided into 5 pages for display.
The third sentence:
This is mainly used for the page turning function. The post parameter curpage of the URL is passed to the curpage variable. This curpage will get the number of pages the viewer wants to reach. (You will understand after running the example)
The fourth sentence:
rs.absolutepage, this is also a built-in attribute, which means to specify the value of the curpage variable as the current page.
Now you can display the records in a loop:
The following is the quoted content:
<%
for i= 1 to rs.pagesize
if rs.eof then
exit for
end if
%>
<%=rs(record_info)%><br>
<%
rs.movenext
next
%>
Second sentence: Use a for loop to display the number of records specified in the rs.pagesize attribute on each page.
The third, fourth and fifth sentences: This sentence means to exit the loop when the last page cannot reach the specified record to avoid errors.
The seventh sentence: Binding the record_info field taken out from the database causes the records in this field to be displayed in a loop.
Sentence 9: Use the rs.movenext method to move the rs record set down one record.
Sentence 10: for loop statement.
In addition, you can use <%=curpage%> to read the current page, use <%=rs.pagecount%> to read the total number of pages, and use <%= rs.recordcount%> to read the total number of records. For example: The current page <%=curpage%> has a total of <%= rs.pagecount%> pages and a total of: <%=rs.recordcount%> records.
In the function of displaying the home page, previous page, next page, and last page, if...else... statements are used, which is easier to understand.
The following is the quoted content:
<%if curpage=1 then%>
front page
<%else%>
<a href=?curpage=1>Homepage</a>
<%end if%>
<%if curpage=1 then%>
Previous page
<%else%>
<a href=?curpage=<%=curpage-1%>>Previous page</a>
<%end if%>
<%if rs.pagecount<curpage+1 then%>
Next page
<%else%>
<a href=?curpage=<%=curpage+1%>>Next page</a>
<%end if%> <%if rs.pagecount<curpage+1 then%>
Last page
<%else%>
<a href=?curpage=<%=rs.pagecount%>>Last page</a>
<%end if%>
Understand:
Home page: This is determined by whether the current page is the first page. If the current page is the first page (that is, the home page), then the word home page will be displayed without a link. Otherwise, a link will be provided to jump directly to the home page.
Previous page: When the current page is the first page, the link is invalid. In turn, the link is to the previous page. Here, use: <%=curpage-1%>, which means subtracting 1 from the current page number to get the previous page. One page.
Next page: You need to use the rs.pagecount attribute for comparison. If the total number of pages is less than the current page number plus 1, it means that this is the last page and the link will be invalid. Otherwise, the link will be to the next page.
Last page: The same function as the next page. When it is determined that it is the last page, the link will be invalid. Otherwise, the current page will be designated as rs.pagecount (total page count).
This tutorial ends here. After the explanation, everyone should have a deeper understanding of ASP paging technology, right? If you have any questions, you can contact me by leaving a message on the blog.