最簡化分頁程式,附源代碼
作者:tank 日期:2004-12-29
最簡化分頁程式,附源代碼
pagetest.asp 表現頁面
<!--#include virtual="/conn.asp"-->
<!--#include virtual="/object.asp"-->
<%
strSql = "select * from authors"
set rs=server.CreateObject("ADODB.RecordSet")
rs.open strSql,conn,1,1
set page = new PageBase
page.pagesize = 3
if not isnumeric(request("page")) then
page.currentpage=1
else
if (cint(request("page")<1)) then
page.currentpage=1
else
page.currentpage=request("page")
end if
end if
page.linkurl="pagetest.asp"
page.show(rs)
page.FenYe(rs)
%>
object.asp 頁對象
<%
'***********************************
'*
'* 尼德類
'*
'* 主要有:分頁類。。。。。
'*
'* coolwind QQ :1294420
'*
'***********************************
class PageBase
dim pagesize '每頁顯示的記錄數
dim currentpage'當然頁面數
dim linkurl '鏈結URL
sub show(oRs)
'顯示分頁符號的子程序---------------------------------------------------------------
i=0
rs.Move pagesize*(currentpage-1)
do while not ors.eof and i<pagesize
response.write ors(1)&"<br>"
i=i+1
oRs.movenext
loop
End sub
'顯示分頁符號的子程序---------------------------------------------------------------
sub FenYe(oRs)
totalput = ors.recordcount
pagesum = totalPut \ PageSize
'如果是第一頁
response.write "第"¤tpage&"頁/共"&pagesum&"頁"
maxpage = 0
minpage = 0
if((currentpage-5)>1) then
minpage = currentpage-5
if (currentpage+4)>pagesum then
maxpage = pagesum
else
maxpage = currentpage+4
end if
else
minpage = 1
if pagesum>10 then
maxpage = 10
else
maxpage = pagesum
end if
end if
if(cint(currentpage-1)>0) then
if(cint(currentpage-1)>cint(pagesum)) then
response.write "<a href="&linkurl&"?Page="&pagesum&" ><<上一頁</a>"
else
response.write "<a href="&linkurl&"?Page="¤tpage-1&" ><<上一頁</a>"
end if
end if
for i=minpage to maxpage
if(cint(i)=cint(currentpage)) then
response.write "["&i&"]"
else
response.write "[<a href="&linkurl&"?Page="&i&" >"&i&"</a>]"
end if
next
if(cint(pagesum-currentpage)>0) then
response.write "<a href="&linkurl&"?Page="¤tpage+1&">下一頁>></a>"
end if
response.write "<br>"
'以下是另一種分頁顯示界面
if currentpage=1 then
Response.Write " [最前頁]"
Response.Write "[上一頁]"
else
Response.Write "[<a href="&linkurl&"?page=1 class='01'>最前頁</a>]"
Response.Write "[<a href="&linkurl&"?Page="¤tpage-1&" class='01'>上一頁</a>]"
End If
'如果是最后一頁
If cint(currentpage)<cint(pagesum) Then
Response.Write "[<A HREF="&linkurl&"?Page="¤tpage+1&" class='01'>下一頁</a>]"
Response.Write "[<A HREF="&linkurl&"?Page="&pagesum&" class='01'>最末頁</a>]"
else
Response.Write "[下一頁]"
Response.Write "[最末頁]"
End If
end sub
end class
%>
conn.asp 數據庫鏈結
<%
'***********************************
'*
'*
'* 與MSSQL數據庫連接
'*
'* coolwind QQ :1294420
'*
'***********************************
dim conn
dim connstr
connstr="provider=sqloledb;data source=servername;uid=sa;pwd=;database=pubs;"
set conn=server.createobject("ADODB.CONNECTION")
if err.number<>0 then
err.clear
set conn=nothing
response.write "數據庫連接出錯!"
Response.End
else
conn.open connstr
if err then
err.clear
set conn=nothing
response.write "數據庫連接出錯!"
Response.End
end if
end if
%>