javascript asp教程第八课--request对象(2)

We'll be using Request.Form when we "Post" an HTML form to the server. Notice that the NAME attribute in the HTML form corresponds to the "name" in Request.Form("name"). To be more specific, <INPUT TYPE="Text" NAME="WebPageVariable"> corresponds with Request.Form("WebPageVariable"). We already talked about the need for the new String( ) constructor back in Lesson 03.

QueryString:

We'll be using Request.QueryString when we use an HTML form to "Get" a page from the server. Request.QueryString() is very similar to Request.Form(). Take a look at script08b.asp which I printed below.

<%@LANGUAGE="JavaScript"%>
<%
var QueryVariable = new String( Request.QueryString("QueryVariable") )
%>
<HTML>
The QueryString Value is: <%=QueryVariable%> <BR>
<%
if (QueryVariable != "Lesson 08's new Query!")
	{
	QueryVariable="Lesson 08's new Query!"
	QueryVariable=escape(QueryVariable)
%>
<A HREF="script08b.asp?QueryVariable=<%=QueryVariable%>">Click Here</A> 
for the link to <I>script08b.asp?QueryVariable=<%=QueryVariable%></I>
<%
	} //closing bracket for if statement.
%>
</HTML>

If you haven't already, Click Here to run script08.asp in a new window. Cycle through all the forms and links, and then come back.

You can use Request.QueryString in two different ways. You can either use an HTML form to "Get" a page from the server, which will generate a query string. Or you can manually build a query string and add it to the backside of a link. We'll dissect script08b.asp from top to bottom.

var QueryVariable = new String( Request.QueryString("QueryVariable") )

The line above in script08b.asp corresponds to the line below from script08a.asp

<INPUT TYPE="hidden" VALUE="<%=monthlySalary%>" NAME="QueryVariable">

The NAME="someName" in the HTML form becomes the Request.QueryString("someName") on the next page.

About half way into script08b.asp are the lines I reprinted below.

<%
if (QueryVariable != "Lesson 08's new Query!")
	{
	QueryVariable="Lesson 08's new Query!"
	QueryVariable=escape(QueryVariable)
%>

We've already converted Request.QueryString() into a JavaScript string at the top of the script. So, now we can do a string comparison.

If the QueryVariable hasn't already been set equal to "Lesson 08's new Query!" then we do that. Then we use the escape( ) method to convert white space and special characters into Unicode. (URL's should contain neither whitespace, nor most special characters.)

In lesson 14 we'll see a better way to encode URL's. When we study the Server Object, we'll see Server.URLEncode(). But for now, just know that escape() works.

You can have more than one QueryString on each page. If you lose count of your QueryStrings, then you use Request.QueryString.Count to tell you the number.

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/3208.html