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所示: