Sometimes, do you feel bothered by writing some repetitive code? As an ASP program, many times to use pagination, repeat the error of the page more than said, and affect the enthusiasm for the program, so write a page function, convenient for everyone to call. As follows:
<%
' ================= programmname: Paging function =======================
' ================ programmed by Bubuy =====================
' ==================== 2003.10.16 ========================
function Cutpage (sqlstr,conn,datenums,pagenums,urls) ' Use of ADO paging functions
Dim sql,cn,datenum,pagenum,url,rsdate
Sql=trim (SQLSTR) ' Gets the SQL statement.
Set cn=conn ' Get data Object
Datenum=cint (datenums) ' Gets the number of records per page
Pagenum=cint (pagenums) ' Get current page number
Url=trim (URLs) ' Get path
Set rsdate=server.createobject ("ADODB. Recordset ")
Rsdate.pagesize=datenum
Rsdate.open sql,cn,1,1
IF rsdate.eof Then
Response.Write ("<center><font stlye= ' font-size:14px; ' color= ' #ff0000 ') > Sorry, no record! </font></center> ")
Else
IF pagenum= "" or pagenum<1 Then
Pagenum=1
ElseIf Pagenum>rsdate.pagecount Then
Pagenum=rsdate.pagecount
End IF
Rsdate.absolutepage =pagenum
Dim Recordhead,recordlast ' defines the beginning and ending records of the current page
Recordhead=1
If pagenum>1 Then recordhead=datenum* (pageNum-1)
If Pagenum>=rsdate.pagecount Then
Recordlast=rsdate.recordcount
Else
Recordlast=datenum*pagenum
End If
Response.Write ("<table width=100% border= ' 0 ' cellpadding= ' 0 ' cellspacing= ' 0 ' style= ' font-size:12px;") > ")
Response.Write ("<tr><td height=38 align=left><font stly= ' font-size:12px;") > ("&recordHead&"-"&recordLast&" section, a total of "&rsDate.Recordcount&", each page shows "&dateNum&" ) </font></td></tr> ")
Dim Urla ' definition of the input URL contains a variable
Dim uppage,downpage,allpage ' defines a variable that is up and down
Dim allwrite ' defines output
Uppage=pagenum-1
Downpage=pagenum+1
Urla=split (URL, "?", -1,1)
If Urla (0) =url Then
Uppage= "<a href=" & URL & "Page=" & Uppage & "Stlye= ' font-size:12px;" > Prev </a> "
If pagenum=1 Then uppage= ""
Downpage= "<a href=" & URL & "Page=" & downpage & "Stlye= ' font-size:12px;" > next Page </a> "
If pagenum=rsdate.pagecount Then downpage= ""
Else
Uppage= "<a href=" & URL & "&page=" & Uppage & "Stlye= ' font-size:12px"; > Prev </a> "
If pagenum=1 Then uppage= ""
Downpage= "<a href=" & URL & "&page=" & downpage & "Stlye= ' font-size:12px"; > next Page </a> "
If pagenum=rsdate.pagecount Then downpage= ""
End If
Allwrite=uppage & downpage & "Total" & rsdate.pagecount & "page" & "Current section" & Pagenum & "Page"
allwrite= "<font style= ' font-size:12px; ' > "& Allwrite &" to <input type= ' text ' style= ' width:30px; ' > Page <input type= ' submit ' value= ' Go ' ></font> '
Response.Write ("<tr><form name= ' formpage ' method= ' post ' action=" &URL& "><td height=30 Right style= ' font-szie=12px; ' > "& allwrite &" </td></form></tr> ")
Response.Write ("<tr><td align=center>")
Response.Write ("<table width=100% border= ' 0 ' cellpadding= ' 0 ' cellspacing= ' 0 ' style= ' font-size:12px;") > ")
Response.Write ("<tr>")
Response.Write ("<tr bgcolor= ' #efffce ' height=25>")
Dim Id,i
For i=0 to rsdate.fields.count-1 ' Set table header
Response.Write ("<td align= ' center ' ><font style= ' font-size:14px;") ><b> "&rsdate.fields (i). name& "</b></font></td>")
Next
Response.Write ("</tr>")
Id=0
While not rsdate.eof and Id<datenum
Id=id+1
If ID Mod 2=0 Then
Response.Write ("<tr bgcolor= #f7f6e7 >")
Else
Response.Write ("<tr bgcolor= #ffffff >")
End If
For each fils in Rsdate.fields
Response.Write ("<td align= ' center ' height=20>" &fils& "</td>")
Next
Response.Write ("</tr>")
Rsdate.movenext
Wend
Response.Write ("<tr height=25 bgcolor= ' #efffce ' >")
For i=0 to Rsdate.fields.count-1 ' set footer
Response.Write ("<td align= ' center ' ><font style= ' font-size:14px;") ><b> "&rsdate.fields (i). name& "</b></font></td>")
Next
Response.Write ("</tr>")
Response.Write ("</table></td></tr>")
Response.Write ("<tr><form name= ' formPage1 ' method= ' post ' action=" &URL& "><td height=30 Right> "& Allwrite &" </td></form></tr> ")
End IF
Rsdate.close
Set rsdate=nothing
End Function
%>
Use this
Page=request ("page")
Sql= "SELECT * from [table]" ' Write SQL statement
Url= "xx.asp" can be "Xx.asp?bubuy=1", of course.
Conn Everybody knows what it's defined.
This is the call.
Call Cutpage (Sql,conn, "3", Page,url) ' 3 represents 3 records per page