ASP中遍历和操作Application对象的集合

ASP中遍历和操作Application对象的集合,第1张

ASP中遍历和操作Application对象的集合,第2张

为了能够正常使用,必须把它们放到服务器上的一个虚拟应用程序内,并且把所提供的global.asa文件放到该应用程序的根目录中。最简单的办法是把global.asa文件放到缺省Web网站的根目录(缺省情况下是C:/InetPub/WWWRoot)中。

  对任何已有的global.asa文件重命名是一个好办法,可以在以后对该文件进行恢复。

  1. 显示Application集合的内容

  ASPCounter对象是StaticObjects集合的一个成员(通过<OBJECT>元素进行定义),但是其余部份(由Server.CreateObject实例化)是Contents集合的成员。

  可以看到使用global.asa例子网页放到这些集合中的值,这在前面已经看到:

  <!-- Declare instance of the ASPCounter component with
  application-level scope //-->
  <OBJECT ID=”ASPCounter” RUNAT=”Server” SCOPE=”Applicatoin”
  PROGID=”MSWC.Counters”>
  </OBJECT>
  ...
  ...
  <SCRIPT LANGUAGE=”VBScript” RUNAT=”Server”>
  Sub Application_onStart()
  ‘Create an instance of an ADO Connection with application-level scope
  Set Application(“ADOConnection”) = Server.CreateObject(“ADODB.Connection”)
  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) = “Application object”
  Application(“Variant_Array”) = varArray ‘Store it in thd Application
  Application(“Start_Time”) = CStr(Now) ‘Store the date/time as a string
  Application(“Visit_Count”) = 0 ‘Set counter variable to zero
  End Sub
  ...
  ...
  </SCRIPT>
  (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
  2、对集合的操作

  1) 增加值到Contents集合

  增加值到Contents集合的方法,与在global.asa网页的脚本代码中使用过的方法相同。允许把一个新的Variant值增加到Application对象中,并已有建议的名字和值(可根据需要进行编辑),单击按钮,重新载入这个网页,把值增加到Application.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方法是明智的。

位律师回复
DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
白度搜_经验知识百科全书 » ASP中遍历和操作Application对象的集合

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情