Sunday, February 6, 2011

Response, Request & Query Strings

Requests between servers and browsers are handled via HTTP - Hypertext Transfer Protocol.

HTTP is a stateless protocol. This means that each request is serviced as it comes; after the request is processed, all of the data is discarded. No state is maintained across requests even from the same client. In a nutshell, data from one page is generally unavailable on the next page etc.

Response is a property of the HttpResponse object. This represents the outgoing data from the current Web form object. We can use the Redirect method to navigate to a new Web form. 

response.redirect("nextpage.aspx")

Request is a property of the HttpRequest object. This represents incoming data to the current Web form. The information stored in the Request object collections originates from the client

We can read the properties of the HttpRequest object to learn more about the client and the page currently being viewed.

lblURL.Text = Request.Url.ToString 

Use Query Strings to send data to the next page

A Query String is composed of field-value pairs and is appended to a URL.

response.redirect("pagename.aspx?FieldName=Value")

The above URL contains a querystring containing a single name/value pair.

Here is another example:

response.redirect("pagename.aspx?FieldName1=Value1&FieldName2=Value2")

Query Strings are useful for passing non sensitive data between 2 pages. The querystring begins with the question mark (?) character. Each name value pair is delineated by an ampersand (&) character.

When using the concatenation character to build a querystring, you need to really pay attention to the ampersand as you are using it in 2 contexts:

1) as the delineator in the querystring
2) as the concatenation operator in the string as it is being built

For instance:


In the above querystring, there are 3 name/value pairs. Each value for each name, is supplied by a string variable. The string is built using the concatenation operator - the ampersand, however, each subsequent name/value pair is also separated by the ampersand character. Concentrate & you will be fine (see what I did there?). LOL.

Read Query Strings on the next page

We can use the request object to read the values of the fields within the Query String.

Dim X as String = Request.QueryString("FieldName")


or as per the above example:


Dim strFirstName as String = Request.QueryString("Firstname")
Dim strLastName as String = Request.QueryString("Lastname")
Dim strPostCode as String = Request.QueryString("PostCode")



The following series of screenshots show values from textboxes being concatenated into a querystring & then displayed on the following page:

1.

 2.

 3.

 4.

 5.

Get a copy of the project here :))

No comments:

Post a Comment