Pages

Wednesday, December 12, 2012

State Management Techniques

The following are the commonly used state management techniques.
  • QueryString
  • Coockies
  • Cache
  • ViewState
  • Session state
  • Application state
  • Static variables
  • Profiles 
 Query String:

Query String will looks like

www.aspnet.com/article.aspx?id=1

In URL which comes after ? symbol is called QueryString.It will be having key,value pair.

In above example 'id' is the key and '1' is the value.we can send multiple values using QueryString

Response.Redirect("www.aspnet.com/article.aspx?id=1&name=satya");

In article.aspx page we can get id and name values as follows

Request.QueryString["id"];
Request.QueryString["name"];

It is most useful State Management technique,It is very easy to use,it will not consume any server side resources.

Disadvantages:

  • URL length limitation is there,we can't send more information through URL.
  • Information send through URL can be visible to anyone,and can be easily altered.

Cookies:

Cookies are Client Side State Management technique.

In ASP.NET we will create Cookies using HttpCookie class.Using Response,Request objects we will add and retrieve Cookies respectively.

Create Cookie:

Response.Redirect["id"].Value="10"

It will stay till browser closes,once browser colsed Cookie will expire.We can create Cookie which will stay based on time.

By making use of HttpCookie.Expires property we can set the expiry date.See below

Response.Cookies["id"].Value="10";
Response.Cookies["id"].Expires=DateTime.Now.AddDays(1);

so that 'id' value will be statying till one day.If we want to remove before that expiry date ,we need to write Expires statement once again as follows

Response.Cookies["id"].Expires=DateTime.Now.AddDays(-1);

Reading the Cookie Values:

Request.Cookies["id"].Value;





0 comments:

Post a Comment