對任何已有的global.asa文件重命名是一個好辦法,可以在以後對該文件進行恢復。
1. 顯示Application集合的內容
ASPCounter對象是StaticObjects集合的一個成員(通過<OBJECT>元素進行定義),但是其餘部份(由Server.CreateObject實例化)是Contents集合的成員。
可以看到使用global.asa例子網頁放到這些集合中的值,這在前面已經看到:
<!-- Declare instance of the ASPCounter component with application-level scope //--> <OBJECT ID=”ASPCounter” RUNAT=”Server” SCOPE=”Applicatoin” PROGID=”MSWC.Counters”> </OBJECT> ... ... <SCRIPT LANGUAGE=”VBScript” RUNAT=”Server”> Sub Application_onStart() ‘Create an instance of an ADO Connection with application-level scope Set Application(“ADOConnection”) = Server.CreateObject(“ADODB.Connection”) Dim varArray(3) ‘Create a Variant array and fill it varArray(0) = “This is a” varArray(1) = “Variant array” varArray(2) = “stored in the” varArray(3) = “Application object” Application(“Variant_Array”) = varArray ‘Store it in thd Application Application(“Start_Time”) = CStr(Now) ‘Store the date/time as a string Application(“Visit_Count”) = 0 ‘Set counter variable to zero End Sub ... ... </SCRIPT> |
(1) 遍歷Contents集合的代碼
爲了遍歷Contents集合,可使用一個For Each ... Next結構。集合中的每一項可以是一個簡單的Variant類型變量、一個Variant數組或者一個對象的引用。因爲需要對每種類型的值進行不同的處理,所以就不得不對每一個進行檢查來判別其類型。
在VBScript中可使用VarType函數完成這個工作。這裏使用IsObject和IsArray函數代替:
For Each objItem in Application.Contents If IsObject(Application.Contents(objItem)) Then Response.Write “Object reference: ‘” & objItem & “’ ” ElseIf IsArray(Application.Contents(objItem)) Then Response.Write “Array: ‘” & objItem & “’ contents are: ” VarArray = Application.Contents(objItem) ‘Note: the following only works with a one-dimensional array For intLoop = 0 To UBound(varArray) Response.Write “ Index(“ & intLoop & “) = “ & _ VarArray(intLoop) & “ ” Next Else Response.Write “Variable: ‘” & objItem & “’ = “ _ & Application.Contents(objItem) & “ ” End If Next |
注意程序如何從Application對象檢索該數組。將其分配給一個局部(Variant)變量,使用下面的語句:
varArray = Application.Contents(objItem) |
使用UBound函數可以查找出數組的大小(元素的數量),這個值可以作爲遍歷的終止條件:
For intLoop = 0 UBound(varArray) |
這個例子是一維數組,並將只顯示這樣的一個數組的內容。可根據需要編輯代碼以處理多維數組,例如:
For intLoop = 0 To UBound(varArray) IntNumberOfDimensions = UBound(varArray, 1) For intDimension = 0 To intNumberOfDimensions Response.Write “ Index(“ & intLoop & “) = “ _ & varArray(intLoop, intDimension) Next Response.Write “” Next |
(2) 遍歷StaticObjects集合的代碼
StaticObjects集合包含了所有在global.asa中使用<OBJECT>元素聲明的對象引用。因爲每個條目都是一個對象變量,可用簡單些的代碼對這個數組進行遍歷。我們將輸出對象的名字(在ID屬性中原有的定義):
For Each objItem in Application.StaticObjects If IsObject(Application.StaticObjects(objItem)) Then Response.Write “<OBJECT> element: ID=’” & objItem & “’ ” End If Next |
2、對集合的操作
1) 增加值到Contents集合
增加值到Contents集合的方法,與在global.asa網頁的腳本代碼中使用過的方法相同。允許把一個新的Variant值增加到Application對象中,並已有建議的名字和值(可根據需要進行編輯),單擊按鈕,重新載入這個網頁,把值增加到Application.Contents集合中,並且在列表中顯示。
增加新的Contents條目的代碼
所有的按鈕和其他HTML控件放置在示例網頁中的一個窗體上。ACTION設置了當前網頁的路徑,提交該窗體時,重新裝入。METHOD屬性爲“POST”,所以控件中的值出現在Request.Form集合中。在以前的章節中採用過這兩種技術:
<FORM ACTION=”<% = Request.ServerVariables(“SCRIPT_NAME”) %>” METHOD=”POST”> |
該窗體上的按鈕都是普通的HTML INPUT控件,具有相同的標題(三個空格)但名字不同。例如,創建第一個按鈕(把值增加到Application對象中)的代碼是:
<INPUT TYPE=”SUBMIT” NAME=”cmdAdd” VALUE=” ”> |
重新載入該網頁時,檢查Request.Form集合,判定單擊的是哪個SUBMIT按鈕,並進行相應的處理。如果是增加一個值到Application對象的按鈕(該按鈕在HTML的<INPUT>元素中被命名爲cmdAdd),使用下面的程序段:
If Len(Request.Form("cmdAdd")) Then strVarName = Request.Form("txtVarName") strVarValue = Request.Form("txtVarValue") Application.Lock Application("strVarName") = strVarValue Application.Unlock End If |
注意程序如何使用Application.Lock和Application.Unlock方法,確保這些值不會因兩個用戶併發地訪問而產生混亂。如果只是對一個特定的值進行設置,一般不可能發生這種情況。但一直使用Lock和Unlock方法是明智的。
2) Contents集合中刪除值
通過遍歷Contents集合(如前面我們所做的)執行ASP網頁時,創建該列表。但是,我們僅收集每項的名字並把它們放到<SELECT>列表元素內的<OPTION>元素中:
… <SELECT NAME=”lstRemove” SIZE=”1”> <% For Each objItem in Application.Contents Response.Write “<OPTION>” & objItem & “</OPTION>” Next &> </SELECT> … |
該ASP代碼執行以後,在瀏覽器中看到的結果是:
<SELECT NAME=”lstRemove” SIZE=”1”> <OPTION>ADOConnection</OPTION> <OPTION>Variant_Array</OPTION> <OPTION>Start_Time</OPTION> <OPTION>Visit_Count</OPTION> <OPTION>My_New_Value</OPTION> </SELECT> |
(1) 刪除單個值
當單擊按鈕刪除單個值時,該窗體再次提交給相同的網頁,但是這一次將設立一個爲cmdRemoveThis的SUBMIT按鈕,然後調用Application.Contents集合的Remove方法:
If Len(Request.Form("cmdRemoveThis")) Then strToRemove = Request.Form("lstRemove") Response.Write "strToRemove = " & strToRemove Application.Lock Application.Contents.Remove(strToRemove) Application.Unlock End If |
注意這是Contents集合的一個方法,而不是Application對象的。語法是Application.Contents.Remove,而不是Application.Remove。
從Contents集合中刪除Start_Time值的結果。
(2) 刪除所有的值
如果單擊三個SUBMIT類型按鈕中的最後一個,該網頁中的代碼將檢測到單擊的按鈕爲cmdRemoveAll,將執行 Application.Contents集合的RemoveAll方法:
If Len(Request.Form("cmdRemoveAll")) Then Application.Lock Application.Contents.RemoveAll Application.Unlock End If |
再次提醒,這是Contents集合的一個方法,而不是Application。語法是Application.Contents.RemoveAll,而不是Application.RemoveAll。