多参数以及多值的Query字段处理

你可以通过Query字段传递不止一个键值对,在传递多个参数时,仅需要使用连接符(&)就可以了。下面的例子传递了两个参数:

<a href=response.asp?firstparam=<%=Server.URLEncode(This is the first parameter.)%>&<Secondparam=<%=Server.URLEncode(This is the second parameter.)%>>点击这里</a>

这个连接的Query字段就包括了两个参数(键值对),键名分别是FirstParamSecondParam。值分别对应于”This is the first parameter.”和”This is the second parameter., URLEncode()方法被用来将这些值在传递中保持正确格式。

Response.asp中,你可以用下面的方法显示两个参数:

<p><%=Request.QueryString(FirstParam)%>

<p><%=Request.QueryString(SecondParam)%>

可以看出,接受信息还是老方法,输出结果为:

This is the first parameter.

This is the second parameter.

你一样可以利用一个参数对应多个值,只需要在query字段中将参数名称写多遍就可以了,例如下面这个例子:

<A HREF=response.asp?OnlyParam=<%=Server.URLEncode(This is the first value of the only parameter.)%>&OnlyParam=<%=Server.URLEncode(This is the second value of the only parameter.)%>>点击这里</a>

在这里面,参数(键)OnlyParam对应于两个值,分别是This is the first value of the only parameter.”和“This is the second value of the only parameter.”,那么Count属性又可以在这里面来检查到底有多少个值,同时下面的例子用For Each来显示所有的值。

这个OnlyParam参数有<%=Request.QueryString(OnlyParam).Count%>个值,

<p>分别是:

<%

FOR EACH pvalue IN Request.QueryString(OnlyParam)

Response.Write(<br>&pvalue)

NEXT

%>

FOR EACH循环会将所有值全部显示出来,如果这个参数是不存在的,那么count属性会显示0个值同时循环不会执行。