欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP 3.0高级编程(十九)

程序员文章站 2023-02-20 23:49:53
5.3.2 dictionary对象示例        本书提供了一系列示例文件可用来试验脚本运行时间库的各...
5.3.2 dictionary对象示例
       本书提供了一系列示例文件可用来试验脚本运行时间库的各种属性。
       本章代码的缺省页面提供了一系列可使用的vbscript示例链接。有些示例对jscript同样有效。这些示例存放在chapter05目录下相应的子目录里,显示的界面如图5-2所示:

图5-2  asp脚本运行期对象示例页面
       要查看dictionary对象的运行,在菜单页面点击第一个链接,打开名叫show_dictionary.的页面。这个页面显示了我们提供的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 programmers 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 users 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> &nbsp; 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="&nbsp;&nbsp;&nbsp;">
  &nbsp; 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