QueryString

   

You often need to pass parameters from one page to another. You may offer users a list of hyperlink choices. The hyperlinks might all go to the same page. To give the destination page enough information so that it can determine the correct action for each request, parameters may be submitted.

For ASP.NET applications, parameters are appended to the destination document's URL. Immediately after the URL, a ? character is added to indicate that there are indeed parameters. Then for each parameter, there is a name and a value, such as "Flavor=Chocolate" . Each parameter must be separated by a & character.

Using Parameters

The following hyperlink goes to Process.aspx, and has two parameters: Name and Address:

 <a href="Process.aspx?Name=Sam&Address=123%20Main%20Street">Sam</a> 

In this example, it's important to point out the %20 set of characters that appears twice. These indicate blank spaces, or ' ' characters. If you simply leave the blank characters without replacing them with %20, the destination URL does not get the entire parameter, but gets only the string up to the first blank character.

If that same previous hyperlink was entered as follows :

 <a href="Process.aspx?Name=Sam&Address=123 Main Street">Sam</a> 

then the Address parameter would contain only the string 123 and not the intended string of 123 Main Street .

The following example shows how to offer users a number of hyperlink selections, all of which have parameters that will be submitted to the destination page:

 <p><a href="QueryStringResult.aspx?Name=Samuel&Color=Red&Flavor=Vanilla& Car=Mustang">    Select this link to see Samuel's preferences</a></p>  <p><a href="QueryStringResult.aspx?Name=George&Color=Green&Flavor=Chocolate& Car=Miata">    Select this link to see George's preferences</a></p>  <p><a href="QueryStringResult.aspx?Name=Suzanne&Color=Blue&Flavor=Strawberry& graphics/ccc.gif Car=Solara">    Select this link to see Suzanne's preferences</a></p>  <p><a href="QueryStringResult.aspx?Name=Willliam&Color=Purple&Flavor=Swirl& Car=BMW">    Select this link to see William's preferences</a></p>  <p><a href="QueryStringResult.aspx?Name=Roger&Color=Brown&Flavor=Mocha& Car=Audi">    Select this link to see Roger's preferences</a></p> 

Note

This code is part of the page that you can find at www.UsingASP.net, selecting Chapter Examples, Chapter 3, then QueryString. You can see the page in Figure 3.12.

Figure 3.12. This screen offers choices in the form of hyperlinks.

graphics/03fig12.gif


To retrieve the value of a parameter, the Request.QueryString() method is used. It returns a string value, and the following simple example shows how this is done:

 Dim strParamData as String  StrParamData = Request.QueryString( "ParamName" ) 

The complete example in Figure 3.12 shows how to offer users several hyperlink selections. When execution is transferred to the destination pages, responses are easily created with calls to Request.QueryString() . This was how all four parameter strings are obtained. The following code shows how this is done in the example on the Web site:

 Name Selected: <%=Request.QueryString( "Name" )%><br>  Favorite Color: <%=Request.QueryString( "Color" )%><br>  Favorite Flavor: <%=Request.QueryString( "Flavor" )%><br>  Favorite Car: <%=Request.QueryString( "Car" )%><br> 

Note

This code is part of the page that is called when a hyperlink is selected. You can see the rendered page in Figure 3.13.

Figure 3.13. The parameters are retrieved with Request.QueryString() .

graphics/03fig13.gif


Parameter Spaces

As mentioned before, parameters with blank spaces aren't interpreted the way you might expect. The blank space character should be replaced with %20 . The following example shows different hyperlink selections, some with blank spaces left and some with the blank spaces replaced by %20 .

 <p><a href="QueryStringSpaceResults.aspx?Name=Samuel&Book=The Cat In The Hat Comes Back">    Select this link to see Samuel's favorite book without explicit URL encoding</a></p>  <p><a href="QueryStringSpaceResults.aspx?Name=Samuel&Book= The%20Cat%20In%20The%20Hat% graphics/ccc.gif 20Comes%20Back">    Select this link to see Samuel's favorite book with URL encoding</a></p>  <p><a href="QueryStringSpaceResults.aspx?Name=Suzanne&Book=Gone With The Wind">    Select this link to see Suzanne's favorite book without explicit URL encoding</a></p>  <p><a href="QueryStringSpaceResults.aspx?Name=Suzanne&Book= Gone%20With%20The%20Wind">    Select this link to see Suzanne's favorite book with URL encoding</a></p> 

QueryString Collections

There might be times when it is easier or more desirable to obtain all the parameters by enumerating the QueryString collection. As with form collections, this technique of QueryString collection enumeration is the most helpful for debugging.

The following example offers a number of hyperlink choices:

 <p><a href="QueryStringCollectionResult.aspx?Name=Samuel&Color=Red&Flavor= graphics/ccc.gif Vanilla&Car=Mustang">    Select this link to see Samuel's preferences</a></p>  <p><a href="QueryStringCollectionResult.aspx?Name=George&Color=Green&Flavor= graphics/ccc.gif Chocolate&Car=Miata">    Select this link to see George's preferences</a></p>  <p><a href="QueryStringCollectionResult.aspx?Name=Suzanne&Color=Blue&Flavor= graphics/ccc.gif Strawberry&Car=Solara">    Select this link to see Suzanne's preferences</a></p>  <p><a href="QueryStringCollectionResult.aspx?Name=Willliam&Color=Purple&Flavor= graphics/ccc.gif Swirl&Car=BMW">    Select this link to see William's preferences</a></p>  <p><a href="QueryStringResult.aspx?Name=Roger&Color=Brown&Flavor=Mocha&Car= Audi">    Select this link to see Roger's preferences</a></p> 

Note

This code is part of the page that you can find at www.UsingASP.net, selecting Chapter Examples, Chapter 3, then QueryString Collection.


The destination URL (QueryStringResult.aspx) then enumerates through the QueryString collection with the following code:

 <%    Dim Item as String    For Each Item in Request.QueryString  %>      Form Control name <font color="red">'<%=Item>'</font><br>      Form Control value <font color="blue">'<%=Request.QueryString(Item)%>'</font><br>  <%    Next  %> 

Note

This code is part of the page that is called when a hyperlink is selected. You can see the rendered page in Figure 3.14.

Figure 3.14. The QueryString collection has been enumerated.

graphics/03fig14.gif


Before we leave this section, I'd like to give you a reference you can use to find the public instance properties and methods of the Request object. Table 3.1 shows you the properties and table 3.2 shows you the methods.

Table 3.1. Public Instance Properties

AcceptTypes

Returns a string array of client-supported MIME accept types. This property is read-only.

ApplicationPath

Gets the virtual path to the currently executing server application.

ApplicationPoolID

Gets the application pool ID for the current URL.

Browser

Provides information about incoming client's browser capabilities.

ClientCertificate

Gets information on the current request's client security certificate.

ConnectionID

Gets the connection ID of the current request.

ContentEncoding

Indicates the character set of data supplied by the client. This property is read-only.

ContentType

Indicates the MIME content type of incoming request. This property is read-only.

Cookies

Gets a collection of client's cookie variables .

FilePath

Indicates the virtual path of the current request. This property is read-only.

Files

Gets the collection of client-uploaded files (Multipart MIME format).

Filter

Gets or sets a filter to use when reading the current input stream.

Form

Gets a collection of Form variables.

Headers

Gets a collection of HTTP headers.

HttpMethod

Indicates the HTTP data transfer method used by client (GET, POST). This property is read-only.

InputStream

Provides access to the raw contents of the incoming HTTP entity body.

IsAuthenticated

Indicates whether the HTTP connection is authenticated.

IsSecureConnection

Indicates whether the HTTP connection is secure (that is, HTTPS). This property is read-only.

Params

Gets a combined collection of QueryString+Form+ ServerVariable+Cookies.

Path

Indicates the virtual path of the current request. This property is read-only.

PathInfo

Indicates additional path information for a resource with a URL extension. That is, for the URL/virdir/page.html/tail, the PathInfo value is /tail. This property is read-only.

PhysicalApplicationPath

Gets the physical file system path of currently executing server application.

PhysicalPath

Gets the physical file system path corresponding to the requested URL. This property is read-only.

QueryString

Gets the collection of QueryString variables.

RawUrl

Gets the current request's raw URL.

RequestType

Indicates the HTTP data transfer method used by client (GET, POST).

ServerVariables

Gets a collection of Web server variables.

TotalBytes

Gets the number of bytes in the current input stream.

Url

Gets information regarding URL of current request.

UrlReferrer

Gets information regarding the URL of the client's previous request that linked to the current URL.

UserAgent

Gets the client browser's raw user agent string.

UserHostAddress

Gets the IP host address of remote client.

UserHostName

Gets the DNS name of remote client.

UserLanguages

Gets a sorted array of client language preferences.

Table 3.2. Public Instance Methods

BinaryRead

Performs a binary read of a specified number of bytes from the current input stream.

Equals (inherited from Object )

Determines whether the specified Object is the same instance as the current Object.

GetHashCode (inherited from Object )

Serves as a hash function for a particular type, suitable for use in hashing algorithms and data structures such as a hash table.

GetType (inherited from Object )

Gets the Type of the Object .

MapImageCoordinates

Maps an incoming image field form parameter into appropriate x/y coordinate values.

MapPath

Overloaded. Maps virtual path (in requested URL) to physical path on server for current request.

SaveAs

Saves an HTTP request to disk.

ToString (inherited from Object )

Returns a String that represents the current Object .

   


Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net