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 ParametersThe 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& 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.
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() .
Parameter SpacesAs 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% 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 CollectionsThere 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= Vanilla&Car=Mustang"> Select this link to see Samuel's preferences</a></p> <p><a href="QueryStringCollectionResult.aspx?Name=George&Color=Green&Flavor= Chocolate&Car=Miata"> Select this link to see George's preferences</a></p> <p><a href="QueryStringCollectionResult.aspx?Name=Suzanne&Color=Blue&Flavor= Strawberry&Car=Solara"> Select this link to see Suzanne's preferences</a></p> <p><a href="QueryStringCollectionResult.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 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.
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
Table 3.2. Public Instance Methods
|