Home | HTML | Asp.Net | HTML Probe | .Net Probe
  Contact Us |Sitemap

States in Asp.Net

  Skip Navigation LinksHome>Asp Home>State Management
State Management is an innovative concept in Web Development because it eliminates the drawback of losing state data due to reloading of a web page. State Management helps a developer to preserve the state of the Application on Both server or client end. It also helps to store runtime changes in application.

There are two methods to implement state management to store information.
  1. Client-Side State Management

    • Hidden fields
    • Cookies
    • Query Strings
    • View State

  2. Server Side State Management

    • Application State
    • Session State

Client-Side State Management

  • Hidden fields : Hidden fields are the control that is not visible when a Web application is viewed in the browser. This control acts as a storage area for any page specific storing information.
    You can use Server.Transfer method on the Button Control Click to pass the information from one page to another. A sample code given below…


  • Cookies : Cookies refer to the text files that are used to store small pieces of information of user’s computer such as user id, browser type, operating system etc. These cookies are stored on the client’s computer. Cookies can either be temporary or persistent.

    Temporary cookies are stored in the browser memory and when the browser is closed, all temporary cookies added to the browser are lost. These cookies are also called session cookies.

    A persistent cookie is saved as a text file in the file system of the client computer. Persistent cookies are used to store information for a long time. A developer can set the expiration date for persistent cookie.

  • Query Strings : Query strings are the information string added at the end of a URL to maintain the state of a Web application. Query strings are not secure because anyone can see the string value through the URL.
    A developer can pass the information from one page to another by using Response.Redirect method. A sample code snippet is given below :


    Here string value is stored in the variable name variableName and is send to the targetPageName.aspx page. The information stored in the variable variableName can be retrieve on the targetPageName.aspx page using the syntax given below on the page load event.

    String s = Request.QueryString["variableName"];
    Label1.Text = s;
  • View state : View state stores page specific information, when a page is posted back to the server. When a page is processed, the current state of the page and its control is hashed into a string and saved as a hidden field on the page. Such a state is called view state.

    The view state is implemented with the help of a hidden field called _VIEWSTATE. This hidden form field is automatically created in every web page. The hidden field of a view state is placed at the top of each page. The source code to maintain a ViewState contains the input tag with three attribute type, name and value and the value of the type attribute is set to hidden.

    Storing information in view state

    A developer can store a key-value pair, valueName=1, in view state as

    viewState["valueName"] = 1;

    Retrieving information from view state

    A developer can retrive stored view state information by using a code snippet as given below:

    int valueName;
    valueName = (int)ViewState["valueName"];

    By default the ViewState property of a web page or control is enabled. Yoy can also enabled or disable the ViewState property of a Web page or any control on the page by setting the EnableViewState property set to true or false.

Server Side State Management

Server-side state is a situation in which developer wants to store the state information at the server.
  • Application state: Application state is used to store the application specific information such as variables and objects. In application state, the data is stored once and read several times. Application state uses the HttpApplicationState class to store and share information throughout the application. HttpApplication class property is used to access the information stored in the application state. HttpApplicationState class provides a lock method which is used to ensure that only one user is able to access and modify the data of an application at any time.

    Storing and Removing information from the application state:

    By creating the objects and variables, you can store the information in application state. A sample code snippet is given below:

    Application["VariableName"] = "Declare value here";

    In the above code snippet Declare value here is the value of the variable, named VariableName which is stored in the application state.

    You can remove an existing variable or object by using the Application.Remove(); method as a code snippet is given below :

    To remove all the variables or objects in application state use the Application.RemoveAll();

  • Session state: Session state is used to store session specific information for web application. Session state is defined in the <sessionState> element of the web.config file. In session state, each user will have a different session state variable. When a user accesses a web page, a session ID for the user is created. This session ID is transferred between the server and the client over the HTTP protocol using cookies. By default a session variable is active for 20 minutes without any user interaction.

    The following code snippet adds the variable named VariableName, with the value Declare value here in the session state:

    Session["VariableName"] = "Declare value here";

    Retrieve the value of the variable by using the code snippet given below:

    String s = (string)Session["VariableName"];

    Configuring Session State Management:Session state can be configured through the web.config file for the application.

         mode="Off|InProc|StateServer|SQLServer|Custom" cookieless="UseUri" timeout="14" />

    Mode Attribute:
    Mode attribute specify the storage location of session state. The mode attribute can have the following values :

    InProc : It is used to store the sessions in the ASP.NET in-memory cache. Session.End event is raised only in this mode.

    off : Session state is disabled in this mode.

    SQLServer : It is used to store sessions in Microsoft SQL Server database and is configured with aspnet_regsql.exe.

    Custom : Specifies custom location to store session state.

    StateServer : It is used to stores sessions in the ASP.NET State Server service aspnet_state.exe.

    timeout Attribute: Timeout attribute is used to abandoning the session state after the number of minutes described in the <sessionState> element.

    cookieless Attribute: Cookieless attribute specifies whether a cookie should be used or not. Different values of cookieless attribute is describe below:

    cookieless = "UseUri" : It specifies that cookies are never used.

    cookieless = "AutoDetect" : cookies is used if the browser supports cookies.

    cookieless = "UseCookies" : It is the default value of cookieless attribute. It specifies that cookies are always used. If the browser does not support cookies, the session information will be lost.

Postback and Cross-Page-Posting

Postback is the process of sending the data back to the server for processing.

Cross page posting enables you to post the WebPage and WebPage’s control values to another WebPage. Cross page posting is used when we want to receive the response on another page.

DMCA.com Protection Status
Design & Maintain By solvingera group| solvingera.com © 2013-2014 | all right reserved |Privacy Policy | Back to top