(Original vince6799)
Paging in asp code is a bit troublesome. I personally wrote the paging code into two functions during the code writing process. Although the function is not perfect, it should be sufficient for general applications.
<%
'The paging function is divided into two functions
'CalcPage(totalrec,msg_per_page,currentpage,n,rowcount,PageRs) paging calculation function
'PageList(ListType,url,querry,Separator,ListLink) paging list function'paging
calculation function
'totalrec total number of record sets
'msg_per_page The number of records displayed on each page. This variable needs to be assigned in advance when calling CalcPage.
'currentpage current page variable, you need to assign a value to this variable in advance when calling CalcPage
'n total number of pages
'rowcount sets the number of data records per page
'PageRs recordset object
sub CalcPage(totalrec,msg_per_page,currentpage,n,rowcount,PageRs)
n=0 'Set the page number to 0 when there is no record
if currentpage="" then currentpage=0
'PageRs.EOF and PageRs.bof no record
'Not PageRs.EOF Or Not PageRs.BOF recorded
if Not PageRs.EOF Or Not PageRs.BOF then
totalrec=PageRs.recordcount
PageRs.pagesize=msg_per_page
if totalrec mod msg_per_page = 0 then 'Calculate the total number of pages, recordcount: the total number of records of data
n = totalrecmsg_per_page 'n:Total number of pages
else
n = totalrecmsg_per_page+1
end if
if not isnumeric(currentpage) or currentpage="" then currentpage=1
If currentpage <> "" then
currentpage = cint(currentpage)
end if
if currentpage < 1 then
currentpage = 1
end if
if currentpage*msg_per_page > totalrec and not((currentpage-1)*msg_per_page < totalrec) then
currentPage=1
end if
PageRs.absolutepage = currentpage 'absolutepage: Set the pointer to the beginning of a page
rowcount = PageRs.pagesize 'pagesize: Set the number of data records for each page
end if
end sub
%>
<%
'Paging list function
'url jump address
'querry? Parameters after ?
'Separator separator
'ListType paging type
'Type:0 "First page | Previous page | Next page | Last page"
'Type:1 "1 | 2 | 3 | 4 | ..........| Next page"
'Type: 2 "First page | First ten pages | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Next ten pages | Last page"
'Style used by ListLink links
sub PageList(ListType,url,querry,Separator,ListLink)
if Separator="" then Separator="|"
if ListType="" then ListType="0"
select case ListType
case "0"
response.write""¤tpage&"/"&n&"page "
response.write "Total"&totalrec&" pieces of information "
if currentpage <= 1 then
response.write"First page "&Separator&" "
response.write"Previous page "&Separator&" "
else
response.write"<a href="""&url&"?page=1&"&querry&""" class="""&ListLink&""">First page</a> "&Separator&" "
response.write"<a href="""&url&"?page="¤tpage-1&"&"&querry&""" class="""&ListLink&""">Previous page</a> "&Separator&"  ;"
end if
if currentpage = n then
response.write"Next page "&Separator&" "
response.write"Last page "
else
response.write"<a href="""&url&"?page="¤tpage+1&"&"&querry&""" class="""&ListLink&""">Next page</a> "&Separator&"  ;"
response.write"<a href="""&url&"?page="&n&"&"&querry&""" class="""&ListLink&""">Last page</a> "
end if
case "1"
if currentpage < n then
response.write"<a href="""&url&"?page="¤tpage+1&"&"&querry&""" class="""&ListLink&""">Next page</a> "
else
response.write"Next page "
end if
for i=1 to n
if cstr(i)=cstr(currentpage) then
response.write "<b>"&i&"</b>"&" "&Separator&" "
else
response.write"<a href="""&url&"?page="&i&"&"&querry&""" class="""&ListLink&""">"&i&"</a> "&Separator&" "
end if
next
case "2"
PageMerCout=10 'The maximum number of pages that can be turned each time
'Get the maximum page number segment of the record
if n mod PageMerCout=0 then
MaxPageFiled=nPageMerCout
else
MaxPageFiled=nPageMerCout+1
end if
'Determine the page number segment where the current page is located
if currentpage mod PageMerCout =0 then
CurrPageFiled=currentpagePageMerCout
else
CurrPageFiled=currentpagePageMerCout+1
end if
'Get the maximum page number and minimum page number of the current page number segment
MaxPageNo=CurrPageFiled*PageMerCout
MinPageNo=(CurrPageFiled-1)*PageMerCout+1
'Output "first page | first ten pages |"
if currentpage<=1 then
response.write"First page "&Separator&" "
else
response.write"<a href="""&url&"?page=1&"&querry&""" class="""&ListLink&""">First page</a> "&Separator&" "
end if
if CurrPageFiled<=1 then
response.write"First ten pages "&Separator&" "
else
response.write"<a href="""&url&"?page="&MinPageNo-PageMerCout&"&"&querry&""" class="""&ListLink&""">First ten pages</a> "&Separator&"  ;"
end if
'Output the current page number segment
for i=MinPageNo to MaxPageNo
if i<=n then
if cstr(i)=cstr(currentpage) then
response.write "<b>"&i&"</b>"&" "&Separator&" "
else
response.write"<a href="""&url&"?page="&i&"&"&querry&""">"&i&"</a> "&Separator&" "
end if
end if
next
'Output "next ten pages | last page"
if CurrPageFiled>=MaxPageFiled then
response.write"The next ten pages "&Separator&" "
else
response.write"<a href="""&url&"?page="&MaxPageNo+1&"&"&querry&""" class="""&ListLink&""">The next ten pages</a> "&Separator&"  ;"
end if
if currentpage>=n then
response.write"Last page "
else
response.write"<a href="""&url&"?page="&n&"&"&querry&""" class="""&ListLink&""">Last page</a> "
end if
end select
end sub
%>