ASP公用翻頁代碼

來源:互聯網
上載者:User
ASP項目中的公用翻頁模組
在大型的ASP項目中,很多的頁面都涉及到翻頁功能。如果每個頁面都寫一個翻頁的程式的話,這樣的工作即降低了工作效率,也不利於工程的模組化,不能使代碼重用。因此,把翻頁這樣的功能模組化是很有必要的。

設計方法:
1、調用該模組時,只需要傳遞記錄集和每頁顯示的記錄的條數;
2、可以點選連結進行翻頁,也可以直接輸入頁碼,斷行符號後翻頁;
3、不要考慮檔案名稱,程式的每次翻頁都能在當前頁面。

想清楚了上面3個問題,我們的公用翻頁模組就可以動手了。

<%
'+++++++++++++++++++++++++++++++++++++
'◆模組名稱:   公用翻頁模組
'◆文   件   名:   TurnPage.asp
'◆傳入參數:   Rs_tmp   (記錄集),   PageSize   (每頁顯示的記錄條數)
'◆輸   出:   記錄集翻頁顯示功能
'+++++++++++++++++++++++++++++++++++++
'
Sub   TurnPage(ByRef   Rs_tmp,PageSize)   'Rs_tmp   記錄集   ;   PageSize   每頁顯示的記錄條數;
Dim   TotalPage   '總頁數
Dim   PageNo   '當前顯示的是第幾頁
Dim   RecordCount   '總記錄條數
Rs_tmp.PageSize   =   PageSize
RecordCount   =   Rs_tmp.RecordCount
TotalPage   =   INT(RecordCount   /   PageSize   *   -1)*-1
PageNo   =   Request.QueryString   ("PageNo")
'直接輸入頁數跳轉;
If   Request.Form("PageNo")<>""   Then   PageNo   =   Request.Form("PageNo")
'如果沒有選擇第幾頁,則預設顯示第一頁;
If   PageNo   =   ""   then   PageNo   =   1  
If   RecordCount   <>   0   then
Rs_tmp.AbsolutePage   =   PageNo
End   If

'擷取當前檔案名稱,使得每次翻頁都在當前頁面進行;
Dim   fileName,postion
fileName   =   Request.ServerVariables("script_name")
postion   =   InstrRev(fileName,"/")+1
'取得當前的檔案名稱,使翻頁的連結指向當前檔案;
fileName   =   Mid(fileName,postion)  
%>
<table   border=0   width='100%'>  
<tr>  
<td   align=left>   總頁數:<font   color=#ff3333><%=TotalPage%></font>頁
    當前第<font   color=#ff3333><%=PageNo%></font>頁</td>
<td   align="right">  
<%If   RecordCount   =   0   or   TotalPage   =   1   Then  
Response.Write   "首頁|前頁|後頁|末頁"
Else%>
<a   href="<%=fileName%>?PageNo=1">首頁|</a>
<%If   PageNo   -   1   =   0   Then
Response.Write   "前頁|"
Else%>
<a   href="<%=fileName%>?PageNo=<%=PageNo-1%>">前頁|</a>
<%End   If

If   PageNo+1   >   TotalPage   Then
Response.Write   "後頁|"
Else%>
<a   href="<%=fileName%>?PageNo=<%=PageNo+1%>">後頁|</a>
<%End   If%>

<a   href="<%=fileName%>?PageNo=<%=TotalPage%>">末頁</a>
<%End   If%></td>
<td   width=95>轉到第
<%If   TotalPage   =   1   Then%>
<input   type=text   name=PageNo   size=3   readonly   disabled   style="background:#d3d3d3">
<%Else%>
<input   type=text   name=PageNo   size=3   value=""   title=請輸入頁號,然後斷行符號>
<%End   If%>頁
</td>
</tr>
</table>
<%End   Sub%>

當然,大家可以把翻頁的連結做成圖片按鈕,這樣的話也面就更加美觀了。

調用方法:
1、在程式開始或要使用翻頁的地方包含翻頁模組檔案;
2、定義變數:RowCount,每頁顯示的記錄條數
3、調用翻頁過程:Call   TurnPage(記錄集,RowCount)
4、在Do   While   迴圈輸出記錄集的條件中加上"   RowCount   >   0   "   條件
5、在迴圈結束   "Loop前"   加上:   RowCount   =   RowCount   -   1

'-----------------------------------------------------
調用範例:
檔案名稱:News.asp

<%
Dim   Conn,Rs_News
Set   Conn   =   server.CreateObject("ADODB.CONNECTION")
Conn.Open   "cpm","cpm","cpm"

Dim   Sql
Sql   =   "Select   *   from   News"
Set   Rs_News   =   Server.CreateObject("ADODB.RECORDSET")
Rs_News.Open   Sql,Conn,1,3   '擷取的記錄集

'公用翻頁模組開始%>
<!--#include   file=../Public/TurnPage.asp-->
<%
Dim   RowCount
RowCount   =   10   '每頁顯示的記錄條數
Call   TurnPage(Rs_News,RowCount)  
'公用翻頁模組結束%>  

<table   width=100%>
<tr>
<td>新聞編號</td>
<td>新聞標題</td>
<td>發布日期</td>
<tr>
<%
If   Not   Rs_News.eof
Do   while   Not   Rs_News.eof   and   RowCount>0
%>
<tr>
<td><%=Rs_News("ID")%></td>
<td><%=Rs_News("Name")%></td>
<td><%=Rs_News("Date")%></td>
<tr>
<%
RowCount   =   RowCount   -   1
Rs_News.MoveNext
Loop
End   If
%>

<%sub list_page(TotalRec,PageSize)
'TotalTec:總記錄數
'PageSize:每頁顯示多少條記錄
TotalPage=Fix((TotalRec-1)/PageSize)+1   '得到總頁數
CurrentPage=Request("page")  '獲得當前頁
if CurrentPage="" or not isInteger(CurrentPage) then
CurrentPage=1
else
CurrentPage=clng(CurrentPage)
end if
P=(CurrentPage-1) /10
response.write"<form method=GET>頁次:<b>"& CurrentPage &"</b>/<b>"& TotalPage &"</b>頁&nbsp;&nbsp;每頁<b>"&PageSize&"</b> 總記錄:<b>"& TotalRec &"</b></td>"&_
"<td valign=middle><div align=right >分頁:"
if CurrentPage=1 then
response.write "<font face=webdings color=red>9</font>"
else
response.write "<a href='?page=1"' title=首頁><font face=webdings>9</font></a>   "
end if
if p*10>0 then response.write "<a href='?page="&Cstr(p*10)&"' title=上10頁><font face=webdings>7</font></a>   "
response.write "<b>"
for ii=p*10+1 to P*10+10
if ii=currentPage then
response.write "<font color=red>"+Cstr(ii)+"</font> "
else
response.write "<a href='?page="&Cstr(ii)&"'>"+Cstr(ii)+"</a>   "
end if
if ii=TotalPage then exit for
next
response.write "</b>"
if ii<n then response.write "<a href='?page="&Cstr(ii)&"' title=下十頁><font face=webdings>8</font></a>   "
if currentPage=TotalPage then
response.write "<font face=webdings color=red>:</font>   "
else
response.write "<a href='?page="&Cstr(TotalPage)&"' title=尾頁><font face=webdings>:</font></a>   "
end if
response.write "轉到:<input type=text name=Page size=3 maxlength=10  value="& currentpage &"><input type=button value=Go name=submit></form>"
end sub%>

'ASP通用翻頁函數 作者:天地小子 2005.7.7 twt326@163.com
'輸入查詢語句,資料庫名,資料庫連接,當前頁號,連結字串,列表條數,查詢條件(預設為空白)
'顯示上下頁導航連結,有下拉框顯示頁數
'這個函數是根據天地PHP通用翻頁函數1直接轉變的,其中使用了CSDN BBS上一個大蝦的SQL高效分頁方案技術
':)由於這裡沒有找到那篇文章,所以老兄請勿見怪了。配合下面的函數使用起來更方便
sub getnav(dbname,conn,tnowpage,link,pagelistnum,wherewords)
  dim sql,trst,sumrows,pagesum,tmphead,showdown,selectcode,i
  sql="select count(*) as recnum from "&dbname
  if wherewords<>"" then sql=sql & " where " & wherewords
  set trst=Server.CreateObject("adodb.recordset")
  trst.open sql,conn,1,1
  sumrows=trst("recnum")
  pagesum=sumrows\pagelistnum+1
  if sumrows mod pagelistnum=0 then pagesum=sumrows/pagelistnum
  tmphead="<a href="&link&"&page="
  if cint(tnowpage)<2 then
    showdown="<table><form name='selform' method='post' action=''><tr><td>首頁&nbsp;&nbsp;上頁&nbsp;&nbsp;"
  else
    showdown="<table><form name='selform' method='post' action=''><tr><td>"&tmphead&"1>首頁</a>&nbsp;&nbsp;"&tmphead&(tnowpage-1)&">上頁</a>&nbsp;&nbsp;"
  end if
  if cint(tnowpage)<cint(pagesum) then
    showdown=showdown&tmphead&(tnowpage+1)&">下頁</a>&nbsp;&nbsp;"&tmphead&pagesum&">末頁</a>&nbsp;&nbsp;"
  else
    showdown=showdown&"下頁&nbsp;&nbsp;末頁&nbsp;&nbsp;"
  end if
  '擷取下拉框轉向代碼
  selectcode="<script language='javascript'>function gopagenav(page){  location='"&link&"&page='+page;  }</script><select name='selpage' id='selpage' onChange='javascript:gopagenav(this.value);'>"
  for i=1 to pagesum
    selectcode=selectcode&"<option value='"&i&"'"
    if cInt(i)=cint(tnowpage) then selectcode=selectcode&" selected"
    selectcode=selectcode&">= "&i&" =</option>"
  next
  selectcode=selectcode&"</select>"
  showdown=showdown&"第</td><td>"&selectcode&"</td><td>頁,共"&pagesum&"頁&nbsp;&nbsp;總記錄數:"&sumrows&"</td></tr></form></table>"
  response.write showdown
  trst.close
  set trst=nothing
end sub
'根據參數產生查詢語句,查詢某頁資料
'參數:當前頁,每頁顯示條數,資料表名(多個用逗號隔開),需要選擇的欄位名(用逗號隔開),查詢條件(無條件時為空白字串),ID主鍵,排序方式(ASC正序或DESC倒序)
'使用樣本
'  sql=getfysql(page,pagelistnum,"tbl_message,tbl_login","tbl_message.*,tbl_login.l_user","tbl_message.m_lid=tbl_login.l_id","m_id","DESC")
'  getnav "tbl_message",conn,page,"adminmessage.asp?act=",pagelistnum,addnavwords
function getfysql(tnowpage,tpagelistnum,tblname,selfields,wherewords,keyfield,sorttype)
  dim fh,hs
  if wherewords="" then wherewords="1=1"
  fh=">":hs="max"
  if sorttype="DESC" then
    fh="<"
 hs="min"
  end if
  if (tnowpage-1)*tpagelistnum=0 then
    sql="select top "&tpagelistnum&" "&selfields&" from "&tblname&" where "&wherewords&" order by "&keyfield&" "&sorttype
  else
    sql="select top "&tpagelistnum&" "&selfields&" from "&tblname&" where "&wherewords& " and " &keyfield&fh&"(select "&hs&"("&keyfield&") from (select top "&((tnowpage-1)*tpagelistnum)&" "&keyfield&" from "&tblname&" where "&wherewords& " order by "&keyfield&" "&sorttype&") as T) order by "&keyfield&" "&sorttype
  end if
  getfysql=sql
end function

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.