欢迎光临
我们一直在努力

ASP 3.0高级编程(十九)

建站超值云服务器,限时71元/月

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

图5-7 增加一个新方法

结果如图5-8所示:

图5-8 add方法的结果

可以在这个页面中试验dictionary对象的属性和方法,你将会发现什么因素及在什么环境下能引起dictionary对象错误。例如,尝试用与已经存在的一个条目相同的键值增加一个条目,看看会出现什么结果。

5.4 scripting.filesystemobject对象

filesystemobject对象提供对计算机文件系统的访问,它允许我们在代码内操作文本文件、文件夹及驱动器。它是脚本运行期库提供的对象之一,对于服务器asp页面内的vbscript和jscript都有效。如果页面的扩展名为.hta(表示它们是hta的一部分),它也可用在客户端的ie 5中。本节仅讨论在服务器上的asp脚本如何使用filesystemobject对象。

超级文本应用程序(hta)由指定的“受信任的”页面组成,在页面的<head>段里包含<hta: application>元素。例如:

<hta:application id=”objmyapp” applicationname=”myapp”>

这些页面可以使用客户端脚本引擎中的一些不常用特性,这些特性中有filesystemobject对象和textstream对象。关于超级文本应用程序的更多信息,请访问microsoft workshop网站。

可以使用下面的程序创建一个filesystemobject对象实例:

‘ in vbscript:

dim objmyfso

set objmyfso = server.createobject(“scripting.filesystemobject”)

// in jscript:

var objmyfso = server.createobject(‘scripting.filesystemobject’);

<!– server-side with an object element –>

<object runat=”server” scope=”page” id=”objfso”

progid=”scripting.filesystemobject”>

</object>

在asp页面里,增加一个对于filesystemobject类型库的引用是非常有用的。这允许使用它直接定义的内置常数,不用像过去那样用数字等效表达式代替。整个脚本运行期库的类型库可以增加到任何asp页面中,代码如下:

<!– metadata type=”typelib” file=”c:\winnt\system32\scrrun.dll” –>如果你是在另一个目录下安装windows,必须编辑file的属性值。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ASP 3.0高级编程(十九)
分享到: 更多 (0)