6.2.6 Content Rotator組件 Content Rotator組件像一個簡化的Ad Rotator組件,需要提供一種內容調度文件(Content Schedule File)。該文件僅是一種包含文本段和HTML代碼段的文本文件,Content Rotator組件可在頁面中自動顯示其中的一個。調度表文件可以包括任何數量的文本內容條目,并可規定比率,用以控制在返回網頁中每個條目的顯示頻率。 1. 內容調度文件 內容進度文件的結構比Ad Rotator的調度文件簡單,僅需提供要使用的各個文本字符串的列表,由前面加兩個百分號(%%)的語句行說明。為了設置每個條目的比率(決定它出現在返回頁面中的頻率),在雙百分號的后面添加了一個數字,其后還可跟一對前向斜線符(//)添加注釋。如: %% 3 // This is the first entry in the schedule text file For more information, mail us at <A HREF=mailto:feedback@wrox.com>Wrox PRess</A>
%% 4 //This is a multi-line text string <H4>Wrox Press</H4> <UL> <LI> Language Primers <LI> Advanced Programming <LI> Internet applications </UL>
%% 2 Visit us on the <A HREF="http://www.wrox.com">World Wide Web</A>
2. 使用Page Counter組件 下面例子使用JScript語言來說明Page Counter組件的使用,先用Server.CreateObject方法創建對象實例,然后調用Hits方法得到對當前網頁的訪問次數。如果訪問次數達到5次,則把計數器重新設置為0,并且在網頁中放置一個鏈接,以便向訪問者通過成績。 <% // in JScript: var objPageCount = Server.CreateObject('MSWC.PageCounter');
//look for a command sent from the FORM section buttons
// increment the counter and display the current hit count objPageCount.PageHit(); Response.Write('You are visitor number ' + objPageCount.Hits() + '<P>');
// we treat every fifth visitor as a winner if (objPageCount.Hits() == 5) { objPageCount.Reset(); // reset the counter Response.Write('You/'re a lucky winner!<BR>'); Response.Write('<A HREF="winner.asp">Go to our winners page</A><BR>'); } %> 注意在第二個Response.Write語句中,后向斜線(/)的使用是防止JScript引擎把引號看作字符串的結束。 本書提供了一個使用Page Counter組件的示例網頁。然而,由于在每個示例頁面里使用一個VBScript為這個組件示例編寫程序。盡管在一個ASP網頁里,混用腳本語言是可能的,但在SSI的#include文件中使用腳本語言能使網頁緩存出現問題,并會引起不可預測的結果。雖然腳本語言不同,但示例網頁的工作方式上與上述相同。Page Counter組件示例頁面如圖6-10所示:
圖6-10 Page Counter組件示例頁面 該網頁包含的<FORM>段中含有兩個按鈕,兩個按鈕都把窗體提交以重新調入網頁。 <FORM ACTION="<% = Request.ServerVariables("SCRIPT_NAME") %>" METHOD="POST"> <INPUT TYPE="SUBMIT" VALUE=" "> Reload this page<P> <INPUT TYPE="SUBMIT" NAME="cmdReset" VALUE=" "> PageCounter.Reset( ) </FORM> 在網頁的開頭,與其他的示例網頁一樣,在請求中查找相關的按鈕名字。在本頁面中,我們僅對第二個名為cmdReset的按鈕感興趣,如果點擊了該按鈕,運行Page Counter組件的Reset方法,重新設置本頁面的訪問次數為0。 'look for a command sent from the FORM section buttons If Len(Request.Form("cmdReset")) Then objPageCount.Reset 'reset the counter 前面看到的代碼將調用PageHit方法把計數器設置為1,并顯示在網頁里。注意已經使用了Hits和Reset方法而沒有給可選參數提供數值,所以它們都在當前網頁計數器上操作,當然能夠在另一個網頁中使用,并可以對一些網頁的統計結果匯總。
圖6-13 使用Permission Checker組件的示例頁面 鏈接所指向的每一個頁面都位于Chapter06示例目錄下的PermissionChecker子目錄中。Permission Checker組件檢查當前用戶是否有訪問各個網頁的權限。如果用戶確實有訪問這個網頁的權限的話,這個網頁的條目將是一個超級鏈接。 'create an instance of the component Set objPermit = Server.CreateObject("MSWC.PermissionChecker") %> <UL>
<LI> <% If objPermit.HasAccess("PermissionChecker/restricted_1.asp") Then %> <A HREF="PermissionChecker/restricted_1.asp"> <% End If %> Restricted Page Number 1 <% If objPermit.HasAccess("PermissionChecker/restricted_1.asp") Then %> </A> <% End If %> </LI> … .. 'other page links here … </UL> 現在我們有訪問三個有訪問限制的網頁的權限,所以這三個條目都是超級鏈接。然而這里不存在第四個鏈接(它可以被移去或是刪除),因為組件不能使用當前用戶(也就是IUSR_machinename)的帳號來訪問它,因此不顯示為超級鏈接。 注意這里選擇僅移去超級鏈接但留下了鏈接文本,在實際情況中應刪除不可訪問的網頁的整個條目。 如果從PermssionChecker子目錄中的restricted_2.asp文件上刪除Read(和任何其他的)權限,并重新調入網頁,對應的條目將不顯示為一個超級鏈接,如圖6-14所示:
圖6-15 使用MyInfo組件的頁面 首次打開頁面時,創建屬性時使用了一個標志變量SetDefaultValues,該變量存放在用戶的Session對象中。當頁面在每個會話里首次打開時,把屬性參數設置成“缺省”值,之后,把此標志變量設置成Yes。在當前會話中,重新調入頁面時,這個數值將不再重新設置缺省值。 'an instance of the component is already created in global.asa If Not Session("SetDefaultValues") = "Yes" Then 'set the properties if they're not set to the default values, 'i.e. if this is the first time that the page has been run 'during the current user session. objMyInfo.MyManager = "Christina Chan" objMyInfo.MyPhoneExtension = "2851" objMyInfo.MyCarParkingSpace = "4A-17" objMyInfo.MyComputerName = "Priscilla" 'set a session flag to show that the default values hve been set Session("SetDefaultValues") = "Yes" End If 每次調用頁面時,檢查請求中的值是否是用于改變屬性值的按鈕的名稱。這個按鈕在一個包含下拉列表和文本框的<FORM>上。如果發現點擊了這個按鈕,則從下拉列表和文本框里采集數值,然后用這些數值改變相關的屬性值。 'look for a command sent from the FORM section buttons If Len(Request.Form("cmdChange")) Then strPropertyName = Request.Form("lstName") 'get the name of the property strNewValue = Request.Form("txtValue") 'get the new value for the property objMyInfo(strPropertyName) = strNewValue 'set the component property value End If