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

ASP 3.0高级编程(十)

答案:(1) 遍历Contents集合的代码
为了遍历Contents集合,可使用一个For Each ... Next结构。集合中的每一项可以是一个简单的Variant类型变量、一个Variant数组或者一个对象的引用。因为需要对每种类型的值进行不同的处理,所以就不得不对每一个进行检查来判别其类型。
在VBScript中可使用VarType函数完成这个工作。这里使用IsObject和IsArray函数代替:
For Each objItem in Application.Contents
If IsObject(Application.Contents(objItem)) Then
Response.Write “Object reference: ‘” & objItem & “’

ElseIf IsArray(Application.Contents(objItem)) Then
Response.Write “Array: ‘” & objItem & “’ contents are:

VarArray = Application.Contents(objItem)
‘Note: the following only works with a one-dimensional array
For intLoop = 0 To UBound(varArray)
Response.Write “ Index(“ & intLoop & “) = “ & _
VarArray(intLoop) & “

Next
Else
Response.Write “Variable: ‘” & objItem & “’ = “ _
& Application.Contents(objItem) & “

End If
Next
注意程序如何从Application对象检索该数组。将其分配给一个局部(Variant)变量,使用下面的语句:
varArray = Application.Contents(objItem)
使用UBound函数可以查找出数组的大小(元素的数量),这个值可以作为遍历的终止条件:
For intLoop = 0 UBound(varArray)
这个例子是一维数组,并将只显示这样的一个数组的内容。可根据需要编辑代码以处理多维数组,例如:
For intLoop = 0 To UBound(varArray)
IntNumberOfDimensions = UBound(varArray, 1)
For intDimension = 0 To intNumberOfDimensions
Response.Write “ Index(“ & intLoop & “) = “ _
& varArray(intLoop, intDimension)
Next
Response.Write “

Next
(2) 遍历StaticObjects集合的代码
StaticObjects集合包含了所有在global.asa中使用<OBJECT>元素声明的对象引用。因为每个条目都是一个对象变量,可用简单些的代码对这个数组进行遍历。我们将输出对象的名字(在ID属性中原有的定义):
For Each objItem in Application.StaticObjects
If IsObject(Application.StaticObjects(objItem)) Then
Response.Write “<OBJECT> element: ID=’” & objItem & “’

End If
Next
1. 增加值到Contents集合
增加值到Contents集合的方法,与在global.asa网页的脚本代码中使用过的方法相同。示例网页允许把一个新的Variant值增加到Application对象中,并已有建议的名字和值(可根据需要进行编辑),如图3-15所示:

图3-15 增加值到Contents集合的屏幕
单击按钮,重新载入这个网页,把值增加到Application.Contents集合中,并且在列表中显示,如图3-16所示:

图3-16 显示Contents集合内容的屏幕
增加新的Contents条目的代码
所有的按钮和其他HTML控件放置在示例网页中的一个窗体上。ACTION设置了当前网页的路径,提交该窗体时,重新装入。METHOD属性为“POST”,所以控件中的值出现在Request.Form集合中。在以前的章节中采用过这两种技术:
<FORM ACTION=”<% = Request.ServerVariables(“SCRIPT_NAME”) %>” METHOD=”POST”>
该窗体上的按钮都是普通的HTML INPUT控件,具有相同的标题(三个空格)但名字不同。例如,创建第一个按钮(把值增加到Application对象中)的代码是:
<INPUT TYPE=”SUBMIT” NAME=”cmdAdd” VALUE=” ”>
重新载入该网页时,检查Request.Form集合,判定单击的是哪个SUBMIT按钮,并进行相应的处理。如果是增加一个值到Application对象的按钮(该按钮在HTML的<INPUT>元素中被命名为cmdAdd),使用下面的程序段:
If Len(Request.Form("cmdAdd")) Then
strVarName = Request.Form("txtVarName")
strVarValue = Request.Form("txtVarValue")
Application.Lock
Application("strVarName") = strVarValue
Application.Unlock
End If
注意程序如何使用Application.Lock和Application.Unlock方法,确保这些值不会因两个用户并发地访问而产生混乱。如果只是对一个特定的值进行设置,一般不可能发生这种情况。但一直使用Lock和Unlock方法是明智的。
2. Contents集合中删除值
在例子网页的底部有两个按钮,如图3-17所示:

图3-17 显示在网页底部的两个按钮
这两个按钮允许从Application.Contents集合中删除值。第一个按钮从集合中删除单个的指定值,下拉列表框显示的是Contents集合值的名字的列表(记住,不能从StaticObjects集合中删除值,因为它们是静态的)。
通过遍历Contents集合(如前面我们所做的)执行ASP网页时,创建该列表。但是,我们仅收集每项的名字并把它们放到<SELECT>列表元素内的<OPTION>元素中:

<SELECT NAME=”lstRemove” SIZE=”1”>
<%
For Each objItem in Application.Contents
Response.Write “<OPTION>” & objItem & “</OPTION>”
Next
&>
</SELECT>

该ASP代码执行以后,在浏览器中看到的结果是:
<SELECT NAME=”lstRemove” SIZE=”1”>
<OPTION>ADOConnection</OPTION>
<OPTION>Variant_Array</OPTION>
<OPTION>Start_Time</OPTION>
<OPTION>Visit_Count</OPTION>
<OPTION>My_New_Value</OPTION>
</SELECT>
(1) 删除单个值
当单击按钮删除单个值时,该窗体再次提交给相同的网页,但是这一次将查找名为cmdRemoveThis的SUBMIT按钮。如果存在(即单击了这个按钮),则使用列表框的值,调用Application.Contents集合的Remove方法:
If Len(Request.Form("cmdRemoveThis")) Then
strToRemove = Request.Form("lstRemove")
Response.Write "strToRemove = " & strToRemove
Application.Lock
Application.Contents.Remove(strToRemove)
Application.Unlock
End If
注意这是Contents集合的一个方法,而不是Application对象的。语法是Application.Contents.Remove,而不是Application.Remove。
从Contents集合中删除Start_Time值的结果如图3-18所示:

图3-18 删除Start_Time值后的屏幕
(2) 删除所有的值
如果单击三个SUBMIT类型按钮中的最后一个(如图3-18所示),该网页中的代码将检测到单击的按钮为cmdRemoveAll,将执行Application.Contents集合的RemoveAll方法:
If Len(Request.Form("cmdRemoveAll")) Then
Application.Lock
Application.Contents.RemoveAll
Application.Unlock
End If
再次提醒,这是Contents集合的一个方法,而不是Application。语法是Application.Contents.RemoveAll,而不是Application.RemoveAll。
图3-19所示的是从Contents集合中删除所有值的结果(记住在运行时间不能从StaticObjects集合删除项):

图3-19 删除Contents集合中所有值的屏幕

3.3.5 活动中的ASP Session对象
示例网页的第二个示例页面show_session.asp,示范了如何使用Session对象。可在Chapter03子目录中的开始菜单(Default.asp)中打开它。
1. 显示和更新Session集合
Session对象示例页面看起来与刚刚使用过的Application对象示例页面相似。它遍历Session对象的Contents和StaticObjects集合,显示其名字和(可能的话)相应的值。如果把这些值与Application对象页面进行比较,将会看到不同之处。
这里还能够看到客户端IP地址的一些其他值。这是当会话启动时global.asa中的代码从Request.ServerVariables集合中得到的。这个页面还显示四个会话属性的值,如图3-20所示:

图3-20 Session对象显示属性的屏幕
下面是例子中使用的golbal.asa文件的相关段落,它把缺省值增加到图3-20所示的屏幕上所看到的会话中:
...
<!-- Declare instance of the ASPContentLink component
with session-level scope //-->
<OBJECT ID="ASPContentLink" RUNAT="Server" SCOPE="Session"
PROGID="MSWC.NextLink">
</OBJECT>

<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
...
...
Sub Session_onStart()

'Create an instance of the AdRotator component with session-level scope
Set Session("ASPAdRotator") = Server.CreateObject("MSWC.AdRotator")
Dim varArray(3) 'Create a Variant array and fill it
varArray(0) = "This is a"
varArray(1) = "Variant array"
varArray(2) = "stored in the"
varArray(3) = "Session object"
Session("Variant_Array") = varArray 'Store it in the Session
Session("Start_Time") = CStr(Now) 'Store the date/time as a string

'We can access the contents of the Request and Response in a Session_onStart
'event handler for the page that initiated the session. This is the *only*
'place that the ASP page context is available like this.
'as an example, we can get the IP address of the user:
Session("Your_IP_Address") = Request.

上一个:ASP 3.0高级编程(十一)
下一个:ASP 3.0高级编程(九)

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