ASP分頁函數、分頁導航
作者:Eve Cole
更新時間:2009-06-26 18:09:37
程式碼
<%
'//分頁函數
'//psize:每頁顯示的資料數
'//pindex:當前頁碼
'//tbName:表名
'//keyIndex:根據什麼欄位分頁,一般是自增長型別(access資料庫的自動編號)
'//where:查詢條件
'//order:排序條件,預設為" order by " &keyIndex &" desc"
'//總記錄數與總頁數可從傳回的結果集中直接取得,欄位datacount儲存總記錄數,欄位pagecount儲存總頁數
private pindex,datacount,pages
'datacount = 0
'pages = 1
public function changePage(psize,tbName,keyIndex,where,order)
dim sqlstring
pindex = Trim(Request.QueryString("page"))
if not isnumeric(psize) or psize="" then psize=1'//每頁顯示的資料數
if not isnumeric(pindex) or pindex="" then pindex=1'//目前頁碼
if order="" then order=" order by " & keyIndex & " desc"
'//取得總數據數
'dim datacount,pages
set rs=conn.execute("select count(*) as datacount from " & tbName & " where 1=1 " & where)
datacount = rs("datacount")'//總記錄數
rs.close
set rs=nothing
'//計算總頁數
if (datacount mod psize)=0 then
pages=datacount psize
else
pages=datacount psize + 1
end if
'//
if cint(pindex)>pages then pindex=pages
'拼接sql字串
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
'//分頁導航
'//fileName:檔案名稱/目前頁面的話,可以留空
'//argString:分頁參數例如classid=1&tid=16,分頁必須的參數page不必填入
'//pindex:當前頁碼
'//datacount:總記錄數
'//pages:總頁數
'//showMsg:是否顯示分頁資訊,參數為true/false
'//showText:是否顯示首頁、上頁、下頁、末頁的導航,參數為true/false
'//showNumber:是否顯示數字分頁導航,參數為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> 頁")
Response.Write("/分<span style='color:red;'>" & pages & "</span> 頁")
Response.Write("/總<span style='color:red;'>" & datacount & "</span> 筆記錄")
Response.Write("] ")
end if
'//
if showText then
if pindex>1 then
Response.Write("<a href='" & fileName & "?" & argString & "page=1'>[首頁]</a>")
Response.Write(" ")
Response.Write("<a href='" & fileName & "?" & argString & "page=" & pindex - 1 & "'>[上頁]</a>")
else
Response.Write("[首頁]")
Response.Write(" ")
Response.Write("[上頁]")
end if
Response.Write(" ")
if pindex<pages then
Response.Write("<a href='" & fileName & "?" & argString & "page=" & pindex + 1 & "'>[下頁]</a>")
Response.Write(" ")
Response.Write("<a href='" & fileName & "?" & argString & "page=" & pages & "'>[末頁]</a>")
else
Response.Write("[下頁]")
Response.Write(" ")
Response.Write("[末頁]")
end if
end if
'//
if showNumber then
Response.Write(" ")
for i = 4 至 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
%>