当前位置:编程学习 > asp >>

ASP 3.0高级编程(十九)

答案:

5.3.2 Dictionary对象示例
       本书提供了一系列示例文件可用来试验脚本运行时间库的各种属性。
       本章代码的缺省页面提供了一系列可使用的VBScript示例链接。有些示例对JScript同样有效。这些示例存放在Chapter05目录下相应的子目录里,显示的界面如图5-2所示:

图5-2  ASP脚本运行期对象示例页面
       要查看Dictionary对象的运行,在菜单页面点击第一个链接,打开名叫show_dictionary.asp的页面。这个页面显示了我们提供的Dictionary对象的内容,允许试验其属性和方法。屏幕如图5-3所示:

图5-3  Dictionary对象的属性和方法
1.  Dictionary的global.asa文件
随Dictionary对象示例页面提供的文件之一是global.asa。它创建并预先填充了一个会话层作用域的Dictionary对象,因此其内容在页面请求之间不会丢失。一般说来(考虑到可扩展性),这不是一个理想的做法。在这个例子里,可以看到Dictionary的属性和方法的效果。
如果在自己的服务器上下载并安装示例,必须创建一个基于此global.asa文件的虚拟应用程序。或者将其内容添加到缺省站点的根文件夹中的global.asa文件里。在第3章讲述了如何用向导创建虚拟应用程序。然而对于本示例,创建一个虚拟应用程序最简单的方法是在Chapter05示例文件夹内右击dictionary子文件夹,在Properties对话框的Home Directory选项卡里,点击Create按钮,如图5-4所示:

图5-4  创建虚拟应用程序
在这个global.asa文件里,代码使用<OBJECT>元素创建一个会话层作用域的Scripting.Dictionary对象实例。然后在Session_onStart事件处理程序里将一系列值用Add方法放入Dictionary中,并将对Dictionary对象的引用指定给ASP会话变量MyDictionary:
<OBJECT ID="objBookList" RUNAT="SERVER" SCOPE="SESSION"
        PROGID="Scripting.Dictionary">
</OBJECT>

<SCRIPT LANGUAGE="VBScript" RUNAT="SERVER">

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

</SCRIPT>
2.  Dictionary示例页面
在“Scripting.Dictionary Object”主页面里,首要的任务是得到一个会话层作用域的Dictionary对象实例的引用。注意,这个引用是一个对象变量,因此必须在VBScript里使用Set关键字。
然后,检查一下是否得到了一个对象(这是个好习惯),如果没有正确地建立包含global.asa文件的虚拟应用程序,检查一下问题出在哪里。你将看到我们自己的消息代替了ASP的错误消息(但是注意,对于这一操作必须关闭缺省的错误处理)。
<%

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")     &n

上一个:关于内部服务器500错误的文档
下一个:TO:张日,在ASP文件中让客户端下载文本文件!

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,