ASP項目中的公用翻頁模組

來源:互聯網
上載者:User

在大型的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
%>

修正:
<%
If Not Rs_News.eof then
Do while Not Rs_News.eof and RowCount>0
%>

而那個公用模組缺<form>,改後:
<%
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 width="258" align=left> 總頁數:<font color=#ff3333><%=TotalPage%></font>頁
當前第<font color=#ff3333><%=PageNo%></font>頁 總共<%=RecordCount%>條 </td>
<td width="308" align="right"> <div align="center">
<%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%>
</div></td>
<td width=189><form name="form1" method="post" action=""> 轉到第 <% If TotalPage = 1 Then%>
<input type=text name=PageNo size=3 readonly disabled style="background:#d3d3d3">
<input type="submit" name="Submit" value="Go" disabled style="background:#d3d3d3">
<%Else%>
<input type=text name=PageNo size=3 >
<input type="submit" name="Submit" value="Go">
<%End If%>
</form>

</td>
</tr>
</table>
<%End Sub%>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.