To overcome this situation, there are several options added at page and application levels. In this article, we shall explore various concepts related to state management. Introduction to different states in ASP.NET There are four states used for managing states in ASP.NET application. They are as listed below: View State Session State Control State Application State View State View state in ASP.NET provides information for the particular ASP.NET page. If there is a requirement for maintaining the web page, the .NET framework manages it automatically. The information about the view state is serialized into XML and encoded through base. The page performance varies if the data is large. Although the view state is enabled some of the controls do not need it. The view state can be disabled for a particular control. The view state for the control can be disabled by modifying the EnableViewState attribute to false. The view state for a web page can be modified by changing the EnableViewState attribute in the @Page directive. The view state for the complete application can be modified through the <pages> section in config file. Advantages of view state The view state values can be encoded, hashed and compressed. As a result the values are secured. There is no need to implement code for maintaining view state. There is no server resource used for managing the state Disadvantages of view state The data stored for view state is saved into more hidden fields on the web page. The data can be modified by the users. The performance for the web page is reduced as the values are saved in the page itself. The data storage is restricted for the memory of the device used. Consider the following example to demonstrate the view state in ASP.NET Add the following code in the source page of application. Code: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> EmpName:<asp:TextBox ID="txt1" runat="server"></asp:TextBox> <asp:Button ID="btn1" runat="server" Text="Show" onclick="btn1_Click" /> <asp:Button ID="btn2" runat="server" Text="Submit" /> <br/> Value from Viewstate is: <asp:Label ID="lbl1" runat="server"></asp:Label> </div> </form> </body> </html> The following code is added in the code behind file. Code: public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(ViewState["EmpName"] ! = null) { lbl1.Text= ViewState["EmpName"].ToString(); } else { lbl1.Text = "Value not available"; } } protected void btn1_Click(object sender, EventArgs e) { ViewState["EmpName"] = txt1.Text; lbl1.Text = txt1.Text; } } The code is compiled and executed and the following output is displayed. Session State The session state in ASP.NET helps user to save values. The scope of the state is restricted to current browser. Every user has a unique session id. The key/value pair structure is used for saving them. The SessionStateItemCollection object is used for storing the session variables. The Session property is used for exposing the object. The variables are indexed by variable name or integer index. Properties of HttpSessionState class are as listed below. Count: The number of items present in the session state collection CookieMode: The application is configured for the cookieless sessions IsReadOnly: State whether the session is read only Keys: The collection of keys for the values present in the session collection Timeout: The time in minutes used by the provider for ending the session Methods of the HttpSessionState class are listed below. Add: The new item is added to the collection Clear: All the key and values are removed from the collection GetType: The Type of the current instance is obtained RemoveAll: The key and values from the collection are removed ToString: The string representing the current object is removed Advantages of the Session State The events can be raised and used by the application It is consistent with the .NET framework and easy to use Customized session state can be used through the state provider The contents are kept in session state variables using the IIS. The session management events can be created by own application Disadvantage of the Session State The state variables are present in the memory till they are removed or replaced. The server performance is degraded. Consider the following example to demonstrate the use of session state Add the code in the source page of the application Code: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="label1" runat="server"></asp:Label> <br></br> <asp:Label ID="label2" runat="server"></asp:Label> <asp:TextBox ID="text1" runat="server"></asp:TextBox> <br/> <asp:Button ID="btn1" runat="server" Text="Save" onclick="btn1_Click" /> <br/> <asp:HyperLink ID="hyp1" runat="server" Text="Navigate to Session" NavigateUrl="~/Default2.aspx"></asp:HyperLink> </div> </form> </body> </html> Add the following code in the code behind file. Code: protected void btn1_Click(object sender, EventArgs e) { Session["UserName"] = text1.Text.ToString(); label1.Text = "Session saved"; } Add another web page in the application. The following code is added in the source page. Code: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="label1" runat="server"><asp:Label> <br/> <asp:HyperLink ID="hyper1" runat="server" Text="GoTo Page1" NavigateUrl="~/Default.aspx"></asp:HyperLink> </div> </form> </body> </html> In the code behind file, add the following code. Code: using System; using System.Collection.Generic; using System.Linq; using System.Web; public partial class Default2: System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { label1.Text = "Welcome" + Session["UserName"]; } } When the code is compiled and executed, the following output is displayed. Control State The control state data is used for monitoring the control order is working effectively. The ControlState property is used to persist the information specific to the control. Application State The HttpApplication class is used for providing the application state information. The application specific values can be saved in the application state and are managed by the server. The state is a global storage and can be accessed through the pages in the application. The data is present in key/value dictionary for the specific URL. Properties of HttpApplication state are as mentioned below. Count: The number of objects in the HttpApplication class AllKeys: The access keys are retrieved using the collection StaticObjects: The objects present in the <object> tag are accessed Item[Int32]: The single HttpApplicationState object is retrieved by the index Contents: The reference for the HttpApplicationState object Methods of HttpApplication state are listed below. Clear: All the objects are removed from the collection Add: A new object is added to the collection GetType: The Type of the current instance is accessed Remove: The named objects are removed from the collection Set: The value of an object is updated Lock: The variable is accessed for managing the synchronization GetHashCode: The default hash function is accessed The application state data are maintained using the events stated below. Application_Start: It states the start of the applciation Application_End: The end of the application is determined Application_Error: The error present in the application is mentioned Session_Start: The start of the session is specified Session_End: The end of the session is specified Advantages of the Application State The application state is accessed through all the web pages. A single copy can be used for storing the information It is easy for users and provides consistent framework classes. Disadvantages of the Application State The server memory is needed by the application state. The server performance is affected. Once the server process is destroyed, the global data of the application state is removed. The unique values cannot be saved in the application state There are three events provided by ASP.NET application. They are as listed below. Application_Start: It is raised when the application starts the execution. The application variables can be initialized. Application_End: When the application closes, the event is raised. All the application resources for the user are released. Application_Error: When unhandled errors occur, the exception is raised.