- ·上一篇文章:一种效率极高的分类算法(转--非常好,帮助很大对于想做好asp的朋友)
- ·下一篇文章:实现文件下载而不是由ie打开的代码
ASP在Scripting.Dictionary对象的作用是什么?
rrItems = objMyData.Items ‘Get all the items into an array<br>
<br>
For intLoop = 0 To objMyData.Count –1 ‘Iterate through the array<br>
StrThisKey = arrKeys(intLoop) ‘This is the key value<br>
StrThisItem = arrItems(intLoop) ‘This is the item (data) value<br>
Next<br>
<br>
// In JScript<br>
// Get VB-style arrays using the Keys() and Items() methods<br>
var arrKeys = new VBArray(objMyData.Keys()).toArray();<br>
var arrItems = new VBArray(objMyData.Items()).toArray();<br>
<br>
for (intLoop = 0; intLoop < objMyData.Count; intLoop++) {<br>
// Iterate through the arrays<br>
strThisKey = arrKeys[intLoop]; // This is the key value<br>
strThisItem = arrItems[intLoop]; // This is the item (data) value<br>
}<br>
在VBScript里也可以使用For Each … Next语句完成同样的功能:<br>
‘ Iterate the dictionary as a collection in VBScript<br>
For Each objItem in arrItems<br>
Response.Write objItem & “ = “ & arrItems(objItem) & “<BR>”<br>
Next<br>
<br>
<b>5.3.2 Dictionary</b><b>对象示例</b><br>
本书提供了一系列示例文件可用来试验脚本运行时间库的各种属性。<br>
本章代码的缺省页面提供了一系列可使用的VBScript示例链接。有些示例对JScript同样有效。这些示例存放在Chapter05目录下相应的子目录里,显示的界面如图5-2所示:<br>
<br>
图5-2 ASP脚本运行期对象示例页面<br>
要查看Dictionary对象的运行,在菜单页面点击第一个链接,打开名叫show_dictionary.asp的页面。这个页面显示了我们提供的Dictionary对象的内容,允许试验其属性和方法。屏幕如图5-3所示:<br>
<br>
图5-3 Dictionary对象的属性和方法<br>
1. Dictionary的global.asa文件<br>
随Dictionary对象示例页面提供的文件之一是global.asa。它创建并预先填充了一个会话层作用域的Dictionary对象,因此其内容在页面请求之间不会丢失。一般说来(考虑到可扩展性),这不是一个理想的做法。在这个例子里,可以看到Dictionary的属性和方法的效果。<br>
如果在自己的服务器上下载并安装示例,必须创建一个基于此global.asa文件的虚拟应用程序。或者将其内容添加到缺省站点的根文件夹中的global.asa文件里。在第3章讲述了如何用向导创建虚拟应用程序。然而对于本示例,创建一个虚拟应用程序最简单的方法是在Chapter05示例文件夹内右击dictionary子文件夹,在Properties对话框的Home Directory选项卡里,点击Create按钮,如图5-4所示:<br>
<br>
图5-4 创建虚拟应用程序<br>
在这个global.asa文件里,代码使用<OBJECT>元素创建一个会话层作用域的Scripting.Dictionary对象实例。然后在Session_onStart事件处理程序里将一系列值用Add方法放入Dictionary中,并将对Dictionary对象的引用指定给ASP会话变量MyDictionary:<br>
<OBJECT ID="objBookList" RUNAT="SERVER" SCOPE="SESSION"<br>
PROGID="Scripting.Dictionary"><br>
</OBJECT><br>
<br>
<SCRIPT LANGUAGE="VBScript" RUNAT="SERVER"><br>
<br>
Sub Session_onStart()<br>
objBookList.Add "2610", "Professional Active Server Pages 3.0"<br>
objBookList.Add "1274", "Instant JavaScript"<br>
objBookList.Add "2882", "Beginning ASP Components"<br>
objBookList.Add "1797", "Professional ASP Techniques"<br>
objBookList.Add "1835", "AD0 2.0 Programmer''s Reference"<br>
Set Session("MyDictionary") = objBookList<br>
End Sub<br>
<br>
</SCRIPT><br>
2. Dictionary示例页面<br>
在“Scripting.Dictionary Object”主页面里,首要的任务是得到一个会话层作用域的Dictionary对象实例的引用。注意,这个引用是一个对象变量,因此必须在VBScript里使用Set关键字。<br>
然后,检查一下是否得到了一个对象(这是个好习惯),如果没有正确地建立包含global.asa文件的虚拟应用程序,检查一下问题出在哪里。你将看到我们自己的消息代替了ASP的错误消息(但是注意,对于这一操作必须关闭缺省的错误处理)。<br>
<%<br>
<br>
on error resume next '' turn off error handling to test if object exists<br>
<br>
''retrieve Dictionary object from user''s session<br>
Set objMyData = Session("MyDictionary")<br>
<br>
If IsObject(objMyData) Then ''found Dictionary object in Session<br>
…<br>
%><br>
<br>
<P><DIV CLASS="subhead">Iterating the Dictionary with Arrays</DIV><br>
<%<br>
arrKeysArray = objMyData.Keys ''get all the keys into an array<br>
arrItemsArray = objMyData.Items ''get all the items into an array<br>
For intLoop = 0 To objMyData.Count - 1 ''iterate through the array<br>
Response.Write "Key: <B>" & arrKeysArray(intLoop) & "</B> Value: <B>" _<br>
& arrItemsArray(intLoop)& "</B><BR>"<br>
Next<br>
%><br>
…<br>
… Other code and controls go here …<br>
…<br>
<%<br>
Else<br>
<br>
''could not find Dictionary object in the session<br>
Response.Write "Dictionary object not available in global.asa for session"<br>
<br>
End If<br>
%><br>
显示在页面上的Dictionary内容列表是使用Dictionary对象的Key和Items方法创建的两个数组,可使用前面的代码遍历它们。<br>
3. Dictionary页面控件<br>
在Dictionary的内容列表下是一系列的HTML控件,可用于设定Dictionary对象的某些属性和执行各种方法。这些控件全部在一个<FORM>内,其ACTION属性值是本页面,所以窗体的内容提交回本页面。在前面的章节的示例里使用了同样的技术。<br>
在<FORM>段中,改变属性或执行一个方法是通过一个按钮(没有标题)实现的。用于属性和方法的值放入按钮旁的文本框或列表框中。<br>
该页的第一个按钮用于设定Dictionary里的条目的Key属性。这里使用了一个下拉列表,可以选择一个已经存在的Key值。下面的代码创建了页面内该部分的控件。为了填充列表,使用了另外一个遍历Dictionary对象的技术,即For Each … Next语句。代码如下:<br>
…<br>
<FORM ACTION="<% = Request.ServerVariables("SCRIPT_NAME") %>" METHOD="POST"><br>
<br>
<P><DIV CLASS="subhead">The Dictionary Properties</DIV><br>
<INPUT TYPE="SUBMIT" NAME="cmdChangeKey" VALUE=" "><br>
Dictionary.Key ("<br>
<SELECT NAME="lstChangeKey" SIZE="1"><br>
<%<br>
For Each objItem in objMyData<br>
Response.Write
<br>
For intLoop = 0 To objMyData.Count –1 ‘Iterate through the array<br>
StrThisKey = arrKeys(intLoop) ‘This is the key value<br>
StrThisItem = arrItems(intLoop) ‘This is the item (data) value<br>
Next<br>
<br>
// In JScript<br>
// Get VB-style arrays using the Keys() and Items() methods<br>
var arrKeys = new VBArray(objMyData.Keys()).toArray();<br>
var arrItems = new VBArray(objMyData.Items()).toArray();<br>
<br>
for (intLoop = 0; intLoop < objMyData.Count; intLoop++) {<br>
// Iterate through the arrays<br>
strThisKey = arrKeys[intLoop]; // This is the key value<br>
strThisItem = arrItems[intLoop]; // This is the item (data) value<br>
}<br>
在VBScript里也可以使用For Each … Next语句完成同样的功能:<br>
‘ Iterate the dictionary as a collection in VBScript<br>
For Each objItem in arrItems<br>
Response.Write objItem & “ = “ & arrItems(objItem) & “<BR>”<br>
Next<br>
<br>
<b>5.3.2 Dictionary</b><b>对象示例</b><br>
本书提供了一系列示例文件可用来试验脚本运行时间库的各种属性。<br>
本章代码的缺省页面提供了一系列可使用的VBScript示例链接。有些示例对JScript同样有效。这些示例存放在Chapter05目录下相应的子目录里,显示的界面如图5-2所示:<br>
<br>
图5-2 ASP脚本运行期对象示例页面<br>
要查看Dictionary对象的运行,在菜单页面点击第一个链接,打开名叫show_dictionary.asp的页面。这个页面显示了我们提供的Dictionary对象的内容,允许试验其属性和方法。屏幕如图5-3所示:<br>
<br>
图5-3 Dictionary对象的属性和方法<br>
1. Dictionary的global.asa文件<br>
随Dictionary对象示例页面提供的文件之一是global.asa。它创建并预先填充了一个会话层作用域的Dictionary对象,因此其内容在页面请求之间不会丢失。一般说来(考虑到可扩展性),这不是一个理想的做法。在这个例子里,可以看到Dictionary的属性和方法的效果。<br>
如果在自己的服务器上下载并安装示例,必须创建一个基于此global.asa文件的虚拟应用程序。或者将其内容添加到缺省站点的根文件夹中的global.asa文件里。在第3章讲述了如何用向导创建虚拟应用程序。然而对于本示例,创建一个虚拟应用程序最简单的方法是在Chapter05示例文件夹内右击dictionary子文件夹,在Properties对话框的Home Directory选项卡里,点击Create按钮,如图5-4所示:<br>
<br>
图5-4 创建虚拟应用程序<br>
在这个global.asa文件里,代码使用<OBJECT>元素创建一个会话层作用域的Scripting.Dictionary对象实例。然后在Session_onStart事件处理程序里将一系列值用Add方法放入Dictionary中,并将对Dictionary对象的引用指定给ASP会话变量MyDictionary:<br>
<OBJECT ID="objBookList" RUNAT="SERVER" SCOPE="SESSION"<br>
PROGID="Scripting.Dictionary"><br>
</OBJECT><br>
<br>
<SCRIPT LANGUAGE="VBScript" RUNAT="SERVER"><br>
<br>
Sub Session_onStart()<br>
objBookList.Add "2610", "Professional Active Server Pages 3.0"<br>
objBookList.Add "1274", "Instant JavaScript"<br>
objBookList.Add "2882", "Beginning ASP Components"<br>
objBookList.Add "1797", "Professional ASP Techniques"<br>
objBookList.Add "1835", "AD0 2.0 Programmer''s Reference"<br>
Set Session("MyDictionary") = objBookList<br>
End Sub<br>
<br>
</SCRIPT><br>
2. Dictionary示例页面<br>
在“Scripting.Dictionary Object”主页面里,首要的任务是得到一个会话层作用域的Dictionary对象实例的引用。注意,这个引用是一个对象变量,因此必须在VBScript里使用Set关键字。<br>
然后,检查一下是否得到了一个对象(这是个好习惯),如果没有正确地建立包含global.asa文件的虚拟应用程序,检查一下问题出在哪里。你将看到我们自己的消息代替了ASP的错误消息(但是注意,对于这一操作必须关闭缺省的错误处理)。<br>
<%<br>
<br>
on error resume next '' turn off error handling to test if object exists<br>
<br>
''retrieve Dictionary object from user''s session<br>
Set objMyData = Session("MyDictionary")<br>
<br>
If IsObject(objMyData) Then ''found Dictionary object in Session<br>
…<br>
%><br>
<br>
<P><DIV CLASS="subhead">Iterating the Dictionary with Arrays</DIV><br>
<%<br>
arrKeysArray = objMyData.Keys ''get all the keys into an array<br>
arrItemsArray = objMyData.Items ''get all the items into an array<br>
For intLoop = 0 To objMyData.Count - 1 ''iterate through the array<br>
Response.Write "Key: <B>" & arrKeysArray(intLoop) & "</B> Value: <B>" _<br>
& arrItemsArray(intLoop)& "</B><BR>"<br>
Next<br>
%><br>
…<br>
… Other code and controls go here …<br>
…<br>
<%<br>
Else<br>
<br>
''could not find Dictionary object in the session<br>
Response.Write "Dictionary object not available in global.asa for session"<br>
<br>
End If<br>
%><br>
显示在页面上的Dictionary内容列表是使用Dictionary对象的Key和Items方法创建的两个数组,可使用前面的代码遍历它们。<br>
3. Dictionary页面控件<br>
在Dictionary的内容列表下是一系列的HTML控件,可用于设定Dictionary对象的某些属性和执行各种方法。这些控件全部在一个<FORM>内,其ACTION属性值是本页面,所以窗体的内容提交回本页面。在前面的章节的示例里使用了同样的技术。<br>
在<FORM>段中,改变属性或执行一个方法是通过一个按钮(没有标题)实现的。用于属性和方法的值放入按钮旁的文本框或列表框中。<br>
该页的第一个按钮用于设定Dictionary里的条目的Key属性。这里使用了一个下拉列表,可以选择一个已经存在的Key值。下面的代码创建了页面内该部分的控件。为了填充列表,使用了另外一个遍历Dictionary对象的技术,即For Each … Next语句。代码如下:<br>
…<br>
<FORM ACTION="<% = Request.ServerVariables("SCRIPT_NAME") %>" METHOD="POST"><br>
<br>
<P><DIV CLASS="subhead">The Dictionary Properties</DIV><br>
<INPUT TYPE="SUBMIT" NAME="cmdChangeKey" VALUE=" "><br>
Dictionary.Key ("<br>
<SELECT NAME="lstChangeKey" SIZE="1"><br>
<%<br>
For Each objItem in objMyData<br>
Response.Write
