ASP内置对象类型属性介绍

  ASP内置对象Request,Request.Form,Request.Form("id")类型,.item, .items, .count, .content属性介绍

 

  测试这些对象类型和是否包含.item, .items, .count, .content属性源代码

 <%
 On Error Resume Next
 Dim i,biName,arrBI,AI
 biName = "ObjectContext," &_
          "Server," &_
          "Application," &_
          "Application.Contents," &_
          "Application.StaticObjects," &_
          "Session," &_
          "Session.Contents," &_
          "Session.StaticObjects," &_
          "Request," &_
          "Response," &_
          "Server.GetLastError," &_
          "Request.ServerVariables," &_
          "Request.ClientCertificate," &_
          "Request.Cookies," &_
          "Request.Form," &_
          "Request.QueryString," &_
          "Response," &_
          "Response.Cookies," &_
          "Request.Form(""ID"")," &_
          "Request.QueryString(""ID"")," &_
          "Request.Cookies(""ID"")," &_
          "Request.ClientCertificate(""Subject"")," &_
          "Request.ServerVariables(""URL"")," &_
          "Server.CreateObject(""ADODB.Connection"")," &_
          "Server.CreateObject(""ADODB.Recordset"")," &_
          "Server.CreateObject(""Scripting.FileSystemObject"")," &_
          "Server.CreateObject(""Scripting.Dictionary"")"
          
 arrBI = Split(biName,",")
 Response.Write("<table border=1 width='80%' align='center'> "& _
 "<tr><th width='25%' nowrap='nowrap'>TypeName操作</th>" &_
 "<th width='12%' nowrap='nowrap'>TypeName结果</th>" &_
 "<th width='40%' nowrap='nowrap'>是否有" &_
 "Items,Item,Contents,Count属性</th></tr>" & vbNewLine)
 
 For i = 0 To Ubound(arrBI)
     Set AI = Eval(arrBI(i))
     Response.Write("<tr><td align=right>" &_
                    "<span style=""color:red"">"&arrBI(i)&"<"&"/"& _
                    "span> </td><td align=left>" &_
                    " "&TypeName(AI)&"</td>")
     
     
     If Err > 0 Then Err.Clear
     Dim v,hasItem,hasItems
     hasItem = False
     hasItems = False
     hasContents = False
     hasCount = False
     
     v = TypeName(AI("0"))
     If Err > 0 Then
         Err.Clear
     Else
         hasItem = "<font color=red><b>True</b></font>"
     End If
     
     v = TypeName(AI.Items)
     If Err > 0 Then
         Err.Clear
     Else
         hasItems = "<font color=orange><b>True</b></font>"
     End If
     
     v = TypeName(AI.Contents)
     If Err > 0 Then
         Err.Clear
     Else
         hasContents = "<font color=green><b>True</b></font>"
     End If
     
     
     v = TypeName(AI.Count)
     If Err > 0 Then
         Err.Clear
     Else
         hasCount = "<font color=blue><b>True</b></font>"
     End If
     
     Response.Write("<td>item:"& hasItem&" | " &_
                         "items:"& hasItems&" | " &_
                         "contents:"& hasContents&" | " &_
                         "count:"& hasCount&"</td></tr>" & vbNewLine)    
     
 Next
 Response.Write("</table>" & vbNewLine)
 %>
 

输出结果:

TypeName操作 TypeName结果 是否有Items,Item,Contents,Count属性
ObjectContext  IASPObjectContext item:False | items:False | contents:False | count:False
Server  IServer item:False | items:False | contents:False | count:False
Application  IApplicationObject item:True | items:False | contents:True | count:False
Application.Contents  IVariantDictionary item:True | items:False | contents:False | count:True
Application.StaticObjects  IVariantDictionary item:True | items:False | contents:False | count:True
Session  ISessionObject item:True | items:False | contents:True | count:False
Session.Contents  IVariantDictionary item:True | items:False | contents:False | count:True
Session.StaticObjects  IVariantDictionary item:True | items:False | contents:False | count:True
Request  IRequest item:True | items:False | contents:False | count:False
Response  IResponse item:False | items:False | contents:False | count:False
Server.GetLastError  IASPError item:False | items:False | contents:False | count:False
Request.ServerVariables  IRequestDictionary item:True | items:False | contents:False | count:True
Request.ClientCertificate  IRequestDictionary item:True | items:False | contents:False | count:True
Request.Cookies  IRequestDictionary item:True | items:False | contents:False | count:True
Request.Form  IRequestDictionary item:True | items:False | contents:False | count:True
Request.QueryString  IRequestDictionary item:True | items:False | contents:False | count:True
Response  IResponse item:False | items:False | contents:False | count:False
Response.Cookies  IRequestDictionary item:True | items:False | contents:False | count:True
Request.Form("ID")  IStringList item:True | items:False | contents:False | count:True
Request.QueryString("ID")  IStringList item:True | items:False | contents:False | count:True
Request.Cookies("ID")  IReadCookie item:True | items:False | contents:False | count:True
Request.ClientCertificate("Subject")  IRequestDictionary item:True | items:False | contents:False | count:True
Request.ServerVariables("URL")  IStringList item:True | items:False | contents:False | count:True
Server.CreateObject("ADODB.Connection")  Connection item:False | items:False | contents:False | count:False
Server.CreateObject("ADODB.Recordset")  Recordset item:False | items:False | contents:False | count:False
Server.CreateObject("Scripting.FileSystemObject")  FileSystemObject item:False | items:False | contents:False | count:False
Server.CreateObject("Scripting.Dictionary")  Dictionary item:True | items:True | contents:False | count:True


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

备注:

1.对于Request.Form,Request.QueryString,类型为 IRequestDictionary,它们是集合对象,它(们)拥有Item属性及Count方法.

对于Item,这里有参考部分:

Item 属性

Object.Item(natIndex) 

参数

Object
EnumNetworkDrive 或 EnumPrinterConnections 方法的结果,或者由 Environment 或 SpecialFolders 属性返回的对象。
natIndex
要检索的项目。

说明

Item 是每个集合的默认属性。对于 EnumNetworkDrive 和 EnumPrinterConnections 集合来说,索引是一个整数;对 Environment 和 SpecialFolders 集合来说,索引是一个字符串。

如果请求的文件夹 (strFolderName) 不可用,则 WshShell.SpecialFolders.Item (strFolderName) 在 VBScript 中返回 "Empty",而在 JScript 中返回 "undefined"。

 

在这里,对于Request.Form(及Request.QueryString),当被用于输出,+-*/等操作时 (TypeName,VarType等特殊类型操作除外),它相当于Request.Form.Item()(及 Request.QueryString.Item()).它们相应返回接收到的参数字符串

如果要获取查询参数当中有多少个参数,可以如下代码:

<%
Dim itemKey,itemVal
For Each itemKey In Request.QueryString
    itemVal = Request.QueryString.Item(itemKey) '或:Request.QueryString(itemKey)
    Response.Write(itemKey & ": " & itemVal & "<br/>" & vbNewLine)
Next
%>

2.对于Request.Form("id")(及Request.QueryString("id")),类型为 IStringList,它们也是集合对象,它(们)拥有Item属性及Count方法.

如果要获取查询参数当中ID参数项,用以下代码是严重错误的:

复制代码
<%
Dim itemKey,itemVal
For Each itemKey In Request.QueryString.Item("id")
    itemVal = Request.QueryString.Item("id").Item(itemKey) 
    '或:Request.QueryString("id")(itemKey)
    Response.Write(itemKey & ": " & itemVal & "<br/>" & vbNewLine)
Next
%>

 

它不能正确输出各项.因为For each...Next语句对IRequestDictionary及IStringList的元素解析不一样,正确的代码为:

复制代码
<%
Dim itemKey,itemVal,iItem
For iItem = 1 To Request.QueryString.Item("id").Count
    itemKey = iItem
    itemVal = Request.QueryString.Item("id").Item(itemKey) 
    '或:Request.QueryString("id")(itemKey)
    Response.Write(itemKey & ": " & itemVal & "<br/>" & vbNewLine)
Next
%>

 

可见,Request.QueryString及Request.QueryString("id")在key-value方面有着差别.

前者:以各个接收的参数名分别作为item的key名.用for each ... next可正确枚举

后者:从1到count分别作为item的key名.用for each...next不可正确枚举(或不雅观),可用for ..to..next及count结合解决

 

至此,item,count及返回类型方面的归结暂时停止...

然后,对于item(),还有一些简写及其说明必须交代一下:

以下代码执行效果一样:

<%
Response.Write "<br/>" & Request.QueryString 
Response.Write "<br/>" & Request.QueryString()
Response.Write "<br/>" & Request.QueryString.Item
Response.Write "<br/>" & Request.QueryString.Item()
%>

 

以下代码执行效果一样:


<%
Response.Write "<br/>" & Request.QueryString("id")
Response.Write "<br/>" & Request.QueryString("id")()
Response.Write "<br/>" & Request.QueryString("id").Item
Response.Write "<br/>" & Request.QueryString("id").Item()
Response.Write "<br/>" & Request.QueryString.Item("id")
Response.Write "<br/>" & Request.QueryString.Item("id").Item
Response.Write "<br/>" & Request.QueryString.Item("id").Item()
%>

 

以下代码执行效果一样:

<%
Response.Write "<br/>" & Request.QueryString.Count
Response.Write "<br/>" & Request.QueryString().Count
%>

以下代码执行效果一样:

<%
Response.Write "<br/>" & Request.QueryString("id").Count
Response.Write "<br/>" & Request.QueryString.Item("id").Count
%>

 

值得注意的地方:不要以下的错误的代码写法:

<%
Response.Write "<br/>" & Request.QueryString.Item.Count
Response.Write "<br/>" & Request.QueryString.Item().Count
Response.Write "<br/>" & Request.QueryString("id").Item.Count
Response.Write "<br/>" & Request.QueryString.Item("id").Item().Count
%>

即item及item()之后不能接.Count,而item("id")或不用item时才能接.Count

因为不管IRequestDictionary还是IStringList,其Item()的结果为字符串了,不能再进行count调用

Request.QueryString.Item()其结果为所有的查询参数串成的字符串

Request.QueryString.Item("id").Item()或Reuqest.QueryString("id").Item()其结果为所有的查询参数为ID的参数名值串成的字符串

 

其它:Dictionary字典对象集合才有Items()方法,不要与item()或item(key)弄混.

字典的items()方法将返回一个变量数组,以结合for .. to .. next和Ubound()操作

字典同时还有item及count属性

字典符合For each...next操作

 

Application及Session当中还有类似Item的Contents属性,返回集合对象IVariantDictionary.操作性等 同于IRequestDictionary

Application及Session当中还有Item属性,真的吗???(当然不是,是验证函数的问题!!)

 

本文参考:http://msdn.microsoft.com/zh-cn/library/windows/apps/aa650188

来源:http://www.cnblogs.com/dreamyoung/archive/2012/04/04/2432265.html

加支付宝好友偷能量挖...


评论(0)网络
阅读(215)喜欢(0)Asp/VBScript