State Management - Web Forms in C# Part 4

Naimishforu
Posted by in C# category on for Beginner level | Points: 250 | Views : 6989 red flag
Rating: 5 out of 5  
 1 vote(s)

The article gives an overview of State Management techniques in ASP.NET.

Introduction


Web pages are stateless – the values of a page's variables and controls are not preserved on the server. 

HTTP, which looks after the passing of information between server and client, is a stateless protocol. This means that every request and response is treated as standalone. To create interactive web applications, web pages have to be re-created with every round trip.

To overcome this problem, you can use HTTP cookies, embed the page state in the HTTP query string, or embed it as a hidden page element.

ASP.NET offers automated state management capabilities. By default, the page state is sent between the browser and the server as a hidden HTML element. This is done transparently for the developer and the user, and Web Forms behave as though the server were maintaining state.

State information is stored in a field with name __VIEWSTATE of type hidden in an <INPUT> HTML element. Hidden means it is not visible to the user but is processed by the server. 

The value attribute in this field contains a tokenized (compressed) string of alphanumeric characters, holding the variables and controls on the page. The tokenized string can be quite long.


<INPUT type="hidden" name="_VIEWSTATE"
  value="dDwtMTc3ODE1MzY2Nzs7Pm01DEMYsmZyrVkdSYI/k5OSSRI/" />


The state information plus any additional information given by the user, is handled when ASP.NET posts the page back to the client. The state of the controls is refreshed and updated. State information is available to Web Forms object properties.

State information takes two forms: 

  • Session State 
  • Application State

Session State covers information relating to a single visit to a web site by an individual using a single browser.

Application State is information that needs to be accessible to any page in an application.

Important session variables include what a visitor intends to buy, the visitor's name for the purpose of personalizing the pages on the basis of registration details, and the visitor's choices for page display. 

Although the session start time is part of the session state, session expiry is set by the TimeOut property and is 20 minutes by default.

Cookies are related to a specific web site and store information that lasts longer than one session. Prime examples of such information are a visitor's registration details and preferences for advertising and e-mail. 

This information is retrieved from the cookie on the visitor's hard drive when they return to the web site.

Application state variables are used to store frequently used data. It's a centralized site-wide store for variables and is available to all resources (web services, ASP.NET pages and so on) within your web application. 

Examples include help desk and emergency phone numbers.

Thanks and Have Fun!!!!!

Page copy protected against web site content infringement by Copyscape

About the Author

Naimishforu
Full Name: Naimish Pandya
Member Level: Bronze
Member Status: Member,MVP
Member Since: 1/22/2011 7:38:35 AM
Country: India
Thanks, Naimish http://dotnetinterview.zxq.net/ http://naimishpandya.blogspot.in/
http://naimishpandya.blogspot.in/

Login to vote for this post.

Comments or Responses

Posted by: Tripati_tutu on: 2/15/2011 | Points: 25
Good one but it will be better if you will add more different state management techniques.
Posted by: Naimishforu on: 2/15/2011 | Points: 25
Sure...

Thanks

Login to post response

Comment using Facebook(Author doesn't get notification)