Chinaunix首页 | 论坛 | 博客
  • 博客访问: 14480944
  • 博文数量: 5645
  • 博客积分: 9880
  • 博客等级: 中将
  • 技术积分: 68081
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-28 13:35
文章分类

全部博文(5645)

文章存档

2008年(5645)

我的朋友

分类:

2008-04-28 20:37:42

下载本文示例代码
  为了能够正常使用,必须把它们放到服务器上的一个虚拟应用程序内,并且把所提供的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 withapplication-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 scopeSet Application(“ADOConnection”) = Server.CreateObject(“ADODB.Connection”)Dim varArray(3) ‘Create a Variant array and fill itvarArray(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 ApplicationApplication(“Start_Time”) = CStr(Now) ‘Store the date/time as a stringApplication(“Visit_Count”) = 0 ‘Set counter variable to zeroEnd Sub......</SCRIPT>  (1) 遍历Contents集合的代码  为了遍历Contents集合,可使用一个For Each ... Next结构。集合中的每一项可以是一个简单的Variant类型变量、一个Variant数组或者一个对象的引用。因为需要对每种类型的值进行不同的处理,所以就不得不对每一个进行检查来判别其类型。  在VBScript中可使用VarType函数完成这个工作。这里使用IsObject和IsArray函数代替: For Each objItem in Application.ContentsIf IsObject(Application.Contents(objItem)) ThenResponse.Write “Object reference: ‘” & objItem & “’”ElseIf IsArray(Application.Contents(objItem)) ThenResponse.Write “Array: ‘” & objItem & “’ contents are:”VarArray = Application.Contents(objItem)‘Note: the following only works with a one-dimensional arrayFor intLoop = 0 To UBound(varArray)Response.Write “ Index(“ & intLoop & “) = “ & _VarArray(intLoop) & “”NextElseResponse.Write “Variable: ‘” & objItem & “’ = “ _& Application.Contents(objItem) & “”End IfNext  注意程序如何从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 intNumberOfDimensionsResponse.Write “ Index(“ & intLoop & “) = “ _& varArray(intLoop, intDimension)NextResponse.Write “”Next  (2) 遍历StaticObjects集合的代码  StaticObjects集合包含了所有在global.asa中使用<OBJECT>元素声明的对象引用。因为每个条目都是一个对象变量,可用简单些的代码对这个数组进行遍历。我们将输出对象的名字(在ID属性中原有的定义): For Each objItem in Application.StaticObjectsIf IsObject(Application.StaticObjects(objItem)) ThenResponse.Write “<OBJECT> element: ID=’” & objItem & “’”End IfNext共2页。 1 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 withapplication-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 scopeSet Application(“ADOConnection”) = Server.CreateObject(“ADODB.Connection”)Dim varArray(3) ‘Create a Variant array and fill itvarArray(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 ApplicationApplication(“Start_Time”) = CStr(Now) ‘Store the date/time as a stringApplication(“Visit_Count”) = 0 ‘Set counter variable to zeroEnd Sub......</SCRIPT>  (1) 遍历Contents集合的代码  为了遍历Contents集合,可使用一个For Each ... Next结构。集合中的每一项可以是一个简单的Variant类型变量、一个Variant数组或者一个对象的引用。因为需要对每种类型的值进行不同的处理,所以就不得不对每一个进行检查来判别其类型。  在VBScript中可使用VarType函数完成这个工作。这里使用IsObject和IsArray函数代替: For Each objItem in Application.ContentsIf IsObject(Application.Contents(objItem)) ThenResponse.Write “Object reference: ‘” & objItem & “’”ElseIf IsArray(Application.Contents(objItem)) ThenResponse.Write “Array: ‘” & objItem & “’ contents are:”VarArray = Application.Contents(objItem)‘Note: the following only works with a one-dimensional arrayFor intLoop = 0 To UBound(varArray)Response.Write “ Index(“ & intLoop & “) = “ & _VarArray(intLoop) & “”NextElseResponse.Write “Variable: ‘” & objItem & “’ = “ _& Application.Contents(objItem) & “”End IfNext  注意程序如何从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 intNumberOfDimensionsResponse.Write “ Index(“ & intLoop & “) = “ _& varArray(intLoop, intDimension)NextResponse.Write “”Next  (2) 遍历StaticObjects集合的代码  StaticObjects集合包含了所有在global.asa中使用<OBJECT>元素声明的对象引用。因为每个条目都是一个对象变量,可用简单些的代码对这个数组进行遍历。我们将输出对象的名字(在ID属性中原有的定义): For Each objItem in Application.StaticObjectsIf IsObject(Application.StaticObjects(objItem)) ThenResponse.Write “<OBJECT> element: ID=’” & objItem & “’”End IfNext共2页。 1 2 : 下载本文示例代码


ASP中遍历和操作Application对象的集合ASP中遍历和操作Application对象的集合ASP中遍历和操作Application对象的集合ASP中遍历和操作Application对象的集合ASP中遍历和操作Application对象的集合ASP中遍历和操作Application对象的集合ASP中遍历和操作Application对象的集合ASP中遍历和操作Application对象的集合ASP中遍历和操作Application对象的集合ASP中遍历和操作Application对象的集合ASP中遍历和操作Application对象的集合ASP中遍历和操作Application对象的集合ASP中遍历和操作Application对象的集合ASP中遍历和操作Application对象的集合ASP中遍历和操作Application对象的集合
阅读(74) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~