Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Client- and Server-side Solutions to Managing State in ASP.NET : Page 3

Whether you are building a traditional Windows application or a Web-based application, state is what an application knows about the user, their current interaction with the application, and other pieces of global information. Get a full assessment of the best techniques for managing state.


advertisement
Client-Based Technique #2: Query Strings
Using query strings is the next client-based technique to help the Web server "remember" things about a returning Web page. If you have ever looked at the URL line when surfing the Web and you've seen a very long string of characters, odds are you are looking at query string information. Query strings are a very simple and popular technique to pass data from one Web page to the next.

It works like this: data is appended to the end of a page's URL and that page pulls the data off the URL line and uses it. Here is an example of a link to a page that passes a user name to a page, such as:

http://www.takenote.com?username=Jim

You can separate multiple values using an ampersand (&) like this.

http://www.takenote.com?username=Jim&city=Raleigh

You can code a query string in a number of ways. You can code it directly into an HTML hyperlink like this.



<a href="http://www.takenote.com?city="Raleigh"> Select City</a>

Or you can use the Response.Redirect method like this.

Response.Redirect( _ "www.takenote.com?username=Jim")

You can also construct the query string using Web Form control values like this.

Response.Redirect( _ "www.takenote.com?username=" & _ txtName.Text & "&city=" & txtCity.Text)

One issue you likely encounter using Web Form control values is many browsers do not support blanks in a query string. You can use Server.URLEncode to encode all the characters within the URL, like this.

Response.Redirect("www.takenote.com?username=" & _ Server.URLEncode(txtName.Text) & "&city=" & _ Server.URLEncode(txtCity.Text"")

Using the above line and entering Jim Duffy as the username and Los Angeles as the city would produce a URL like this.

http://www.takenote.com? username=Jim%20Duffy &city=Los%20Angeles

Retrieving Query String Values
Grabbing the query string values off the URL line is quite simple. The following code declares two local variables and stores the UserName and City query string variables.

Dim UserName As String Dim City As String UserName = Request.QueryString("UserName") City = Request.QueryString("City")

Advantages of Using Query Strings

  • Query strings are easy to implement.
  • Browser support for passing values in a query string is nearly universal.
  • Query strings are contained in the HTTP request for a specific URL and do not require server resources.
Disadvantages of Using Query Strings
Query strings aren't suitable for all types of information, because:

  • Query strings are insecure because the information in the query string is directly visible to the user on the address line in the browser
  • Many browsers impose a 255 URL character limit which can limit their flexibility.


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date