Sub Session_onStart() objBookList.Add "2610", "Professional Active Server Pages 3.0" objBookList.Add "1274", "Instant javaScript" objBookList.Add "2882", "Beginning ASP Components" objBookList.Add "1797", "Professional ASP Techniques" objBookList.Add "1835", "AD0 2.0 Programmer's Reference" Set Session("MyDictionary") = objBookList End Sub
on error resume next ' turn off error handling to test if object exists
'retrieve Dictionary object from user's session Set objMyData = Session("MyDictionary")
If IsObject(objMyData) Then 'found Dictionary object in Session … %>
<P><DIV CLASS="subhead">Iterating the Dictionary with Arrays</DIV> <% arrKeysArray = objMyData.Keys 'get all the keys into an array arrItemsArray = objMyData.Items 'get all the items into an array For intLoop = 0 To objMyData.Count - 1 'iterate through the array Response.Write "Key: <B>" & arrKeysArray(intLoop) & "</B> Value: <B>" _ & arrItemsArray(intLoop)& "</B><BR>" Next %> … … Other code and controls go here … … <% Else
'could not find Dictionary object in the session Response.Write "Dictionary object not available in global.asa for session"
End If %> 顯示在頁面上的Dictionary內容列表是使用Dictionary對象的Key和Items方法創建的兩個數組,可使用前面的代碼遍歷它們。 3. Dictionary頁面控件 在Dictionary的內容列表下是一系列的HTML控件,可用于設定Dictionary對象的某些屬性和執行各種方法。這些控件全部在一個<FORM>內,其ACTION屬性值是本頁面,所以窗體的內容提交回本頁面。在前面的章節的示例里使用了同樣的技術。 在<FORM>段中,改變屬性或執行一個方法是通過一個按鈕(沒有標題)實現的。用于屬性和方法的值放入按鈕旁的文本框或列表框中。 該頁的第一個按鈕用于設定Dictionary里的條目的Key屬性。這里使用了一個下拉列表,可以選擇一個已經存在的Key值。下面的代碼創建了頁面內該部分的控件。為了填充列表,使用了另外一個遍歷Dictionary對象的技術,即For Each … Next語句。代碼如下: … <FORM ACTION="<% = Request.ServerVariables("SCRIPT_NAME") %>" METHOD="POST">
<P><DIV CLASS="subhead">The Dictionary Properties</DIV> <INPUT TYPE="SUBMIT" NAME="cmdChangeKey" VALUE=" "> Dictionary.Key (" <SELECT NAME="lstChangeKey" SIZE="1"> <% For Each objItem in objMyData Response.Write "<OPTION>" & objItem Next %> </SELECT> ") = " <INPUT TYPE="TEXT" NAME="txtChangeKey" SIZE="15" VALUE="New Key Name"> " <BR> … … Other controls go here … … </FORM> … 4. 使用Dictionary的屬性和方法 在“Scription.Dictionary Object”頁面,點擊用來檢查并改變條目的Key屬性的按鈕,如圖5-5所示:
圖5-5 使用Dictionary的Key屬性 把窗體再次提交給頁面。該頁面包含一個腳本段,檢查被點擊的按鈕的值。它通過在Resquest.Form集合里查找按鈕的名字來斷定單擊的是哪個按鈕。如果發現一個對應于cmdChangKey的值,則從列表中或文本框中得到相應的值并用來改變Key屬性: … 'look for a command sent from the FORM section buttons If Len(Request.Form("cmdChangeKey")) Then strKeyName = Request.Form("lstChangeKey") 'Existing key from list box strNewKey = Request.Form("txtChangeKey") 'New key value from text box objMyData.Key(strKeyName) = strNewKey 'Set key property of this item End If … 頁面重新載入后,在Dictionary的內容列表里能看到相應的結果,如圖5-6所示:
圖5-6 頁面重載后的結果 頁面的其余代碼用來設定一個條目的Item屬性,或者執行Dictionary對象的方法。下面是這些操作的代碼,每段代碼與演示Key屬性的代碼非常類似。每次都將結果顯示在Dictionary的內容列表中: … If Len(Request.Form("cmdChangeItem")) Then strKeyName = Request.Form("lstChangeItem") 'Existing key from list box strNewValue = Request.Form("txtChangeItem") 'New item value from text box objMyData.Item(strKeyName) = strNewValue 'Set the Item property End If
If Len(Request.Form("cmdAdd")) Then strKeyName = Request.Form("txtAddKey") 'New key value from text box strItemValue = Request.Form("txtAddItem") 'New item value from text box objMyData.Add strKeyName, strItemValue 'Execute the Add method End If
If Len(Request.Form("cmdRemove")) Then strKeyName = Request.Form("lstRemove") 'Existion key from list box objMyData.Remove strKeyName 'Execute the Remove method End If
If Len(Request.Form("cmdRemoveAll")) Then objMyData.RemoveAll 'Execute the RemoveAll method End If … 例如,如果現在點擊Add方法的按鈕,在Dictionary的內容列表里將增加一個新的條目,如圖5-7所示: