Request 对象之QueryString集合


QueryString 集合检索 HTTP 查询字符串中变量的值。HTTP 查询字符串由问号 (?) 后的值指定。几个不同的进程都可以生成查询字符串。如,anchor 标记

<A HREF= "example?string=this is a sample">string sample</A>

生成值为 "this is a sample" 的变量名字符串。通过发送表格或由用户在其浏览器的地址框中键入查询也可以生成查询字符串。

语法

Request.QueryString(variable)[(index)|.Count]

参数

variable
在 HTTP 查询字符串中指定要检索的变量名。
index
这是一个可选参数,可以用来检索 variable 的多个值中的某一个值。这可以是从 1 到 Request.QueryString(variable).Count 之间的任何整数。

注释

QueryString 集合是在 ServerVariables 集合中 QUERY_STRING 变量的分析版本 。它可以让您以名称检索 QUERY_STRING 变量。Request.QueryString (参数) 的值是出现在 QUERY_STRING 中所有参数 的值的数组。通过调用 Request.QueryString(parameter).Count 可以确定参数有多少个值。如果变量未关联多个数据集,则计数为 1。如果找不到变量,计数为 0。

要在多个数据集合的一个中引用 QueryString 变量,请指定 index 的值。index 参数可以是 1 到 Request.QueryString(variable).Count 中任意值。如果没有指定 index 的值,引用多个 QueryString 变量中的某个变量时,返回的数据是逗号分隔的字符串。

Request.QueryString 中使用参数时,服务器分析发送给请求的参数,并返回指定的数据。如果应用程序需要未分析的 QueryString 数据,可以通过调用不带参数的 Request.QueryString 检索到这个数据。

可以使用复述符在查询字符串中循环遍历所有的数据值。例如,如果发送以下的请求

http://NAMES.ASP?Q=Fred&Q=Sally

而且 Names.asp 包含下面的脚本,

---NAMES.ASP---
<%
For Each item In Request.QueryString("Q")
  Response.Write item & "<BR>"
Next
%>

Names.asp 将显示如下。

Fred
Sally

上述脚本也可以用 Count 来写。

<%
For I = 1 To Request.QueryString("Q").Count	
  Response.Write Request.QueryString("Q")(I) & "<BR>"
Next
%>

示例

客户端请求

/scripts/directory-lookup.asp?name=fred&age=22

results in the following QUERY_STRING value.

name=fred&age=22.

QueryString 集合将包含 nameage两个成员。那么,您就可以使用下面的脚本。

Welcome,  <%= Request.QueryString("name") %>. 
Your age is  <%= Request.QueryString("age") %>.

将输出

Welcome, Fred. Your age is 22.

如果使用下面的脚本

The unparsed query string is:  <%=Request.QueryString %>

将输出

The unparsed query string is: name=fred&age=22
本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3