State Management Techniques in ASP.NET

asplogo

asp.net

ASP.net is one of the fast growing web languages which a web programmer will came across. The demand of ASP.net developer is increasing day by day in the market. So we manage to bring you some of the tutorials of ASP.net for beginners specially. It would help you get through main part of web development easily and in no time. So in today’s tutorial I would be tell you about how to create View State, Session State and Application State. Please follow each and every step carefully.

Introduction:
This article covers the overview of state-management techniques in Asp.net. There are two types of technique: Client side and server side.
Client side:

Server Side:

Client Side:

Hidden Fields:
To store data on the client side asp.net provides the hidden field controls. They are not visible on the page. If you want to view them you can see source code. All the data is stored in value property.

Cookies:
Cookies are small text files. In order to track the user these files are stored on the user machine. When a user make request for a page the server send a cookie which is stored in the hard drive. If user makes other subsequent requests the browser will check for the cookie and it will be treated as the same user. If the cookies are deleted then it will be dealt as a new user.

Control state:
Another state management technique. When the information has to be stored a custom control is created. It helps to save state information but like view state it can’t be turned off.

View State:

A webpage instance is created when request to the server is generated. The instances get destroyed after a short interval of time. If a form has some data stored on it and the button is clicked. Request to the server is generated but suppose as response if an error occurs and you get back to the previous page. All the data stored will be lost. And you have to redo the work.  So, in order to save the information View State is used. In asp.net it is enabled by default. Apart from this there can be some issues regarding performance and security. Let’s see it how it works with an example
protected void Page_Load(object sender, EventArgs e)

{

if(!IsPostBack)

{

if (ViewState[“Clicks”] == null)

{

ViewState[“Clicks”] = “0”;

}

TextBox1.Text=ViewState[“Clicks”].ToString();

}

 

}

 

protected void Button1_Click(object sender, EventArgs e)

{

 

int ClicksCountt = (int)ViewState[“Clicks”] + 1;

TextBox1.Text = ClicksCountt.ToString();

ViewState[“Clicks”] = ClicksCountt;

}

On the design view a text box and a button is created.  In the page load method a condition is verifying that it should proceed only when it’s not a post back request. View state is checked for the null value and zero is assigned to it. And the value is displayed on the textbox. In the buttons method the value is incremented and stored in int variable. That is displayed on the text box. And view state is assigned the value current value.

Session State:

User information can also be stored with the help of session state. The information stored by the view state is on page whereas the session state stores it on the server. When the user is inactive the information stored will be cleared.  The server doesn’t know anything about the user so a cookie is sent to the user computer that has information about the user session. Session state uses resources. Little problem is that the server does not know that how long a user session will last. So, the default time to expire a session is 20 minutes.

Off Session state is not enabled
Inproc Session state is stored locally
Stateserver Session state is stored on remote server
Sql server Session state stored on SQL server

 

protected void Page_Load(object sender, EventArgs e)

 

{

if (!IsPostBack)

{

if (Session[“FN”] == null & Session[“LN”] == null)

{

Session[“FN”] = “FirstName”;

Session[“LN”] = “LastName”;

TextBox1.Text = Session[“FN”].ToString();

TextBox2.Text = Session[“LN”].ToString();

 

}

}

}

 

protected void Button1_Click(object sender, EventArgs e)

{

Label2.Text = (string)Session[“FN”];

Label3.Text = (string)Session[“LN”];

Session[“FN”] = TextBox1.Text;

Session[“LN”] = TextBox2.Text;

}

 

The code above is simple and it displays default values as first name and last name. On clicking the submit button the values stored in the text boxes will be displayed on the label.

Application State:

Application state is like multiple user global data. An entire class will have one instance but the other application users can’t access it.  The difference is that the application object has information about many users and sessions. If any change is made within a single page  it will be reflected in all the pages.

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

if (Application[“FN”] == null & Application[“LN”] == null)

{

Application[“FN”] = “FirstName”;

Application[“LN”] = “LastName”;

TextBox1.Text = Application[“FN”].ToString();

TextBox2.Text = Application[“LN”].ToString();

 

}

}

}

 

protected void Button1_Click(object sender, EventArgs e)

{

Label2.Text = (string)Application[“FN”];

Label3.Text = (string)Application[“LN”];

Application[“FN”] = TextBox1.Text;

Application[“LN”] = TextBox2.Text;

}

}

Article Written by Waleed Rashid. More info here.

Exit mobile version