Introduction
This article does an overview of state management techniques in ASP.NET. I will be discussing about the various types of state management techniques both client side and server side.
Background
State management means to preserve state of a control, web page, object/data, and user in the application explicitly because all ASP.NET web applications are stateless, i.e., by default, for each page posted to the server, the state of controls is lost. Nowadays all web apps demand a high level of state management from control to application level.
Using the code
Types of state management
There are two types of state management techniques: client side and server side.
Client side
- Hidden Field
- View State
- Cookies
- Control State
- Query Strings
Server side
- Session
- Application
Levels of state management
- Control level: In ASP.NET, by default controls provide state management automatically.
- Variable or object level: In ASP.NET, member variables at page level are stateless and thus we need to maintain state explicitly.
- Single or multiple page level: State management at single as well as multiple page level i.e., managing state between page requests.
- User level: State should be preserved as long as a user is running the application.
- Application level: State available for complete application irrespective of the user, i.e., should be available to all users.
- Application to application level: State management between or among two or more applications.
Client side methods
1. Hidden field
Hidden field is a control provided by ASP.NET which is used to store small amounts of data on the client. It store one value for the variable and it is a preferable way when a variable's value is changed frequently. Hidden field control is not rendered to the client (browser) and it is invisible on the browser. A hidden field travels with every request like a standard control’s value.
Let us see with a simple example how to use a hidden field. These examples increase a value by 1 on every "No Action Button" click. The source of the hidden field control is.
<asp:HiddenField ID="HiddenField1" runat="server" />
In the code-behind page:
protected void Page_Load(object sender, EventArgs e)
{
if (HiddenField1.Value != null)
{
int val= Convert.ToInt32(HiddenField1.Value) + 1;
HiddenField1.Value = val.ToString();
Label1.Text = val.ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//this is No Action Button Click
}
2. View state
View state is another client side state management mechanism provided by ASP.NET to store user's data, i.e., sometimes the user needs to preserve data temporarily after a post back, then the view state is the preferred way for doing it. It stores data in the generated HTML using hidden field not on the server.
View State provides page level state management i.e., as long as the user is on the current page, state is available and the user redirects to the next page and the current page state is lost. View State can store any type of data because it is object type but it is preferable not to store a complex type of data due to the need for serialization and deserilization on each post back. View state is enabled by default for all server side controls of ASP.NET with a property
EnableviewState set to true.
Let us see how ViewState is used with the help of the following example. In the example we try to save the number of postbacks on button click.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
if (ViewState["count"] != null)
{
int ViewstateVal = Convert.ToInt32(ViewState["count"]) + 1;
Label1.Text = ViewstateVal.ToString();
ViewState["count"]=ViewstateVal.ToString();
}
else
{
ViewState["count"] = "1";
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text=ViewState["count"].ToString();
}
3. Cookies
Cookie is a small text file which is created by the client's browser and also stored on the client hard disk by the browser. It does not use server memory. Generally a cookie is used to identify users.
A cookie is a small file that stores user information. Whenever a user makes a request for a page the first time, the server creates a cookie and sends it to the client along with the requested page and the client browser receives that cookie and stores it on the client machine either permanently or temporarily (persistent or non persistence). The next time the user makes a request for the same site, either the same or another page, the browser checks the existence of the cookie for that site in the folder. If the cookie exists it sends a request with the same cookie, else that request is treated as a new request.