ASP paging function, paging navigation
Author:Eve Cole
Update Time:2009-06-26 18:09:37
program code
<%
'//Paging function
'//psize: Number of data displayed on each page
'//pindex: current page number
'//tbName: table name
'//keyIndex: paging according to what field, usually a self-increasing type (automatic numbering of the access database)
'//where:query conditions
'//order: sorting conditions, the default is "order by " &keyIndex &" desc"
'//The total number of records and total pages can be obtained directly from the returned result set. The field datacount stores the total number of records, and the field pagecount stores the total number of pages.
private pindex,datacount,pages
'datacount = 0
'pages = 1
public function changePage(psize,tbName,keyIndex,where,order)
dimsqlstring
pindex = Trim(Request.QueryString("page"))
if not isnumeric(psize) or psize="" then psize=1'//The number of data displayed on each page
if not isnumeric(pindex) or pindex="" then pindex=1'//Current page number
if order="" then order=" order by " & keyIndex & " desc"
'//Get the total number of data
'dim datacount,pages
set rs=conn.execute("select count(*) as datacount from " & tbName & " where 1=1 " & where)
datacount = rs("datacount")'//Total number of records
rs.close
set rs=nothing
'//Calculate the total number of pages
if (datacount mod psize)=0 then
pages=datacount psize
else
pages=datacount psize + 1
end if
'//
if cint(pindex)>pages then pindex=pages
'Splice sql string
if pindex<=1 then
sqlstring="select top " & psize & " *," & datacount & " as datacount," & pages & " as pagecount from " &_
tbName & " where 1=1 " & where & " " & order
else
sqlstring="select top " & psize & " *," & datacount & " as datacount," & pages & " as pagecount from " &_
tbName & " where 1=1 and " & keyIndex & " not in(select top " & (pindex-1)*psize & " " & keyIndex & " from " &_
tbName & " where 1=1 " & where & " " & order & ") " & where & " " & order
end if
'Response.Write(sqlstring)
set changePage=conn.execute(sqlstring)
end function
'//Page navigation
'//fileName: file name/current page, you can leave it blank
'//argString: paging parameters such as classid=1&tid=16, the necessary parameter page for paging does not need to be filled in
'//pindex: current page number
'//datacount: total number of records
'//pages: total number of pages
'//showMsg: Whether to display paging information, the parameter is true/false
'//showText: Whether to display the navigation of the home page, previous page, next page, and last page, the parameters are true/false
'//showNumber: Whether to display digital paging navigation, the parameter is true/false
'public function pageLink(fileName,argString,pindex,datacount,pages,showMsg,showText,showNumber)
public function pageLink(fileName,argString,showMsg,showText,showNumber)
'//
if argString<>"" then argString = argString & "&"
if not showText and not showNumber then showText=true
'//
if showMsg then
Response.Write("[")
Response.Write("<span style='color:red;'>" & pindex & "</span> page")
Response.Write("/分<span style='color:red;'>" & pages & "</span> pages")
Response.Write("/Total<span style='color:red;'>" & datacount & "</span> records")
Response.Write("] ")
end if
'//
if showText then
if pindex>1 then
Response.Write("<a href='" & fileName & "?" & argString & "page=1'>[Home]</a>")
Response.Write(" ")
Response.Write("<a href='" & fileName & "?" & argString & "page=" & pindex - 1 & "'>[Previous page]</a>")
else
Response.Write("[Home]")
Response.Write(" ")
Response.Write("[Previous page]")
end if
Response.Write(" ")
if pindex<pages then
Response.Write("<a href='" & fileName & "?" & argString & "page=" & pindex + 1 & "'>[next page]</a>")
Response.Write(" ")
Response.Write("<a href='" & fileName & "?" & argString & "page=" & pages & "'>[last page]</a>")
else
Response.Write("[next page]")
Response.Write(" ")
Response.Write("[last page]")
end if
end if
'//
if showNumber then
Response.Write(" ")
for i = 4 to 1 step -1
if (pindex - i)>0 then
Response.Write("<a href='" & fileName & "?" & argString & "page=" & pindex - i & "'>" & pindex - i & "</a>")
Response.Write(" ")
end if
next
'//
Response.Write("<span style='color:red;'>" & pindex & "</span>")
'//
for i = 1 to 4
if (pindex + i)<=pages then
Response.Write(" ")
Response.Write("<a href='" & fileName & "?" & argString & "page=" & pindex + i & "'>" & pindex + i & "</a>")
end if
next
'//
end if
'//
end function
%>