改進後的ASP公用翻頁模組
來源:互聯網
上載者:User
翻頁 用DWMX2004的外掛程式包可以實現ASP的各種分頁功能,但是那些代碼實在有點恐怖,我們在實際的項目開發中,有沒有更簡單的方法實現"一勞永逸"呢?答案是肯定的.
我們需要的功能:
1、調用該模組時,只需要傳遞記錄集和每頁顯示的記錄的條數;
2、可以點選連結進行翻頁,也可以直接輸入頁碼,斷行符號後翻頁;
3、不要考慮檔案名稱,程式的每次翻頁都能在當前頁面。
具體編寫內容
大家可以把翻頁的連結做成圖片按鈕,這樣的話也面就更加美觀了。
調用方法:
1、在程式開始或要使用翻頁的地方包含翻頁模組檔案;
2、定義變數:RowCount,每頁顯示的記錄條數
3、調用翻頁過程:CallTurnPage(記錄集,RowCount)
4、在DoWhile迴圈輸出記錄集的條件中加上"RowCount>0"條件
5、在迴圈結束"Loop前"加上:RowCount=RowCount-1
使用範例:
1.
2.我們使用DWMX2004建立一個ASP頁面,內容如下
<%@LANGUAGE="VBSCRIPT"CODEPAGE="936"%>
<!--#includefile="Connections/conn.asp"-->
<!--#includefile="pagein.asp"-->'引入公用翻頁模組
<%'定義記錄集
Dimrsnews
Dimrsnews_numRows
Setrsnews=Server.CreateObject("ADODB.Recordset")
rsnews.ActiveConnection=MM_conn_STRING
rsnews.Source="SELECT*FROMdbo.zooNewsWHEREnisshow=1ORDERBYnaddtimeDESC"
rsnews.CursorType=1
rsnews.CursorLocation=2
rsnews.LockType=1
rsnews.Open()
rsnews_numRows=0
%>
<%'使用重複行為(重複下面的<tr>標籤內容)
DimRepeat2__numRows
DimRepeat2__index
Repeat2__numRows=20
Repeat2__index=0
rsnews_numRows=rsnews_numRows+Repeat2__numRows
%>
<html>
<tablewidth="100%"border="0"cellspacing="0"cellpadding="0">
<tr>
<tdheight="40"align="right">
<%'關鍵在這裡,在重複內容上面產生一個導航條
dimRowCount
RowCount=20
callTurnPage(rsnews,RowCount)
%>
</td>
</tr>
<tr>
<td>
<tablewidth="95%"border="0"align="center"cellpadding="0"cellspacing="0">
<tr>
<tdheight="1"valign="top"class="dotX"><imgsrc="images/spacer.gif"width="1"height="1"></td>
</tr>
<%'重複開始
While((Repeat2__numRows<>0)AND(NOTrsnews.EOF))
%>
<tr>
<tdheight="25"><%=(rsnews.Fields.Item("ntitle").Value)%>
<inputtype="submit"name="Submit"value="Submit">
</td>
</tr>
<%
Repeat2__index=Repeat2__index+1
Repeat2__numRows=Repeat2__numRows-1
rsnews.MoveNext()
Wend'重複結束
%>
<tr>
<tdheight="1"valign="top"class="dotX"><imgsrc="images/spacer.gif"width="1"height="1"></td>
</tr>
</table></td>
</tr>
<tr>
<tdheight="40"align="right">
<%'在重複內容下面產生一個導航條,可選,注意這裡不再需要dimRowCount
RowCount=20
callTurnPage(rsnews,RowCount)
%></td>
</tr>
</table>
</body>
</html>
<%'關閉記錄集
rsnews.Close()
Setrsnews=Nothing
%>
PS:當使用者輸入不存在的頁數時(如小數,負數等)程式將自動過濾.