State Management in ASP.NET
State management is the process by which you maintain state and page information over multiple requests for the same or different pages.
Types of State Management
There are 2 types State Management:
Client – Side State Management
This stores information on the client's computer. The techniques available to store the state information at the client end are listed down below:
a) View State: ViewState is the mechanism that allows state values to be preserved across page postbacks. Values stored in view state will loss with the page.
Example:
ViewState ["clickCounter"] = 1; //set value in ViewState field
int value = (int) ViewState ["clickCounter"]; //get value from ViewState field
b) Hidden fields: – Like view state, hidden fields store data in an HTML form without displaying it in the user's browser. The data is available only when the form is processed.
Example:
<inputtype="hidden"runat="server"id="hdnValue"/> //creating hidden field
hdnValue.Value="admin"; //set value in hidden field
string value=hdnValue.Value; //get value from hidden field
c) Cookies: Cookies store a value in the user's browser that the browser sends with every page request to the same server. Cookies are the best way to store state data that must be available for multiple Web pages on a web site.
Example
HttpCookie cookie = newHttpCookie ("user", "abc"); // create cookie with the name
and value
cookie.Expires = DateTime.Now.AddHours (2); // specifies cookie expires after
two hours
Response.Cookies.Add (cookie); // specifies cookie and its value
is added
HttpCookie cook = Request.Cookies ["user"]; // retrieving cookie information
string val = cook.Value.ToString (); // store the value of cookie
d) Query Strings: Query strings store values in the URL that are visible to the user. Use query strings when you want a user to be able to e-mail or instant message state data with a URL.
Example:
Response.Redirect ("Home.aspx?data=user&id=abc"); //sending two values through
url in the field data and id
respectively.
string strData = Request.QueryString ["data"]; //get value from data field
string strId = Request.QueryString ["id"]; //get value from id field
Server – Side State Management
a) Application State:Application state is stored in memory on the server and is faster than storing and retrieving information in a database. Application state is a useful place to store small amounts of often-used data that does not change from one user to another.
Example:
Application["noVisitors"] = 1; // set value in the field name noVisitors
int val = (int)Application["noVisitors"]; //get value from application field
b) Session State: ASP.NET Session state provides a place to store values that will persist across page requests. Values stored in Session are stored on the server and will remain in memory until they are explicitly removed or until the Session expires.
Example:
Session["emailid"] = "abc@gmail.com"; // set value in the session field
string email = Session["emailid"].ToString(); // get value from the session field
Anonymous User
19-Apr-2019Very nice post.
Anurag Sharma
27-Aug-2011ASP.Net technology offers following state management techniques.
Client side State Management
Cookies
Hidden Fields
View State
Query String
Server side State Management
Session State
Application State