Monday, May 21, 2012

State Management Techniques in ASP.Net-Part1


State Management Techniques in ASP.Net

In this post we discuss various options for state management in web applications, developed in ASP.Net. Generally, web applications are based on stateless HTTP protocol which does not preserve any information associated with the page and controls on each round trip(client - server- client communication). In typical client-server communication using HTTP protocol, a new instance of the Web Page created  in every page request.



What is State Management?
State management is the process by which you maintain state and page information over multiple requests for the same or different pages. For example, if a Textbox of a page contains the value "5" before Postback then maintain this value after Postback is called state management.

Types of State Management

Broadly , we can classify the state management techniques in to two types:-
  • Client-Side State Management
  • Server-Side State Management
Each technique has it's own pros and cons. Let's start to exploring these techniques one by one.

State Management Types

Client-Side State Management

As the name suggests, in Client-Side State Management information stores on the client's computer by embedding information in to the Web Page. ASP.Net provides various client-side state management options like cookies, query string, view state etc.

bandwidth should be considered while using client-side state management because they involve in each round trip to server.For example, Cookies are exchanged between client and server in each page request.

There are different client-side state management options. These options are:-

  1.  View State
  2. Control State
  3. Hidden Fields
  4. Cookies
  5. Query String
Let's start to explore various client-side state management options one by one.

(a.) ViewState- View State can be used to store state information for single user.It is the default method that page uses for preserving page and control property values between round trip(client-server-client communication). View State is the built in property of web controls for preserving data between post backs. We can enable/ disable the view state property of each web control by set EnableViewState  property True/False. When the page is posted, one of the first task preformed by page processing is to save view state.We can disable the View State of entire page by adding EnableViewState=False in @page directives.Example:

//Add value to view state
ViewState[ "UserId" ] =  "Manish" ;
//Reading value from view state
Response.Write(ViewState[ "UserId" ]);

(b.) ControlState- Control State is a new mechanism in ASP.Net which addresses some of the shortcomings of View State. Sometimes we need to store control-state data in order for a control works properly.For example, if you create a custom controls which have different tabs that shows different information in each tab, in order for that control work properly i.e control needs to know which tab is selected between round trip, View State property can be used for this purpose but View State property can be disabled at a page level , effectively breaking your control. To solve this ASP.Net exposes a feature called Control State.
 The ControlState property allows you to persist property information that is specific to a control and cannot be turned off like the ViewState property.

(c.) Hidden Fields- ASP.Net allows you to store data in HiddenField control which renders in standard HTML hidden field.A hidden filed does not render visibly in the browser, but we can set properties of hidden filed as we can with standard controls.The data stored in hidden filed is available only when the form is processed. 

(d.) Cookies- Cookies are small piece of data that stored either in a text file on client's file system or in-memory of client's browser sessions.We can use cookies to store information for particular user or application.Cookies are exchanged between client and server in each page request i.e when browser request a page, client send cookie information along with the request information and server can read the cookie and extract the values.Cookies have a size limitation of 4KB. Storing huge information is not possible. Example:

//Add information in cookie
Response.Cookies[ "UserId" ].Value=myvalue;
//Retrieve value from cookie
if (Request.Cookies["UserId"] != null)   
    lbMessage.text = "Dear" + Request.Cookies["UserId"].Value + ", Welcome to our website!";
else    
   lbMessage.text = "Guest,welcome to our website!";

(e.) Query Strings- A query string is a information that is appended at the end of page URL.An example of URL with query string is:

                    http://www.mysite.com?UserId=5

I the above URL  UserId =5 is called query string. Query string is start with a question mark(?) in URL. Query strings are usually used to send data from one page to other page.

Query Strings provide simple but limited way to maintain information.Most browsers impose a limit of 255 characters on URL length. We can not passes huge information using query string.Example:

//Retrieve value of Query String
if (Request.QueryString["UserId"] != null)    
     lbMessage.text = "Dear" + Request. QueryString ["UserId"].ToString() + ", Welcome to our website!";
else   
    lbMessage.text = "Guest,welcome to our website!";

So we discuss here different types of client side state management option. I am not discussed here the prons and cons of these state management options. I left this on you for R&D .
In next post we will discuss the second type of state management i.e "Server-Side State Management". Till then have a nice study. 

Refrence:-
  1. ASP.NET State Management Overview
  2. State management in ASP.NET






No comments:

Post a Comment

^ Scroll to Top