State Management in Asp.net.


State Management – State Management techniques are used to persist data between post-backs. State Management techniques can be divided in two categories :

  1. Client State Management
    • View state
    • Control state
    • Hidden fields
    • Cookies
    • Query strings
  2. Server State Management
    • Application state
    • Session state
    • Profile properties
    • Database support

View state

  • Use when you need to store small amounts of information for a page that will post back to itself.
  • Syntax:   ViewState[“Name”]=”I am a Software developer”
    To get value from viewstate need to convert data
    string Name = Viewstate[“Name”].tostring();
  • Viewstate is stored in hidden field as string in encrypted form.
  • Viewstate can be enabled/disabled at page level.

Control state

  • Use when you need to store small amounts of state information for a control between round trips to the server.
  • Control state can not be modified
  • It can not be disabled
  • It can not be directly accessed

Hidden fields

  • Use a hidden field only on pages that are submitted to the server.
  • It can store only single value as string.
  • Hidden fields are standard HTML controls that are easy to use.

Cookies

  • Use when you need to store small amounts of information on the client.
    These are two types :
    Persistent – They store in browser’s sub folder and remain till the manually deleted or automatically deleted based on time defined.
    Non-Persistent – They lost when user close browser.

Query strings

  • Use query strings only if you are requesting the same page, or another page via a link.
  • They are not secure as they are visible in browser and hackers can tempered them.
  • Get value from querystring :
    Request.QueryString[“Text”]

Application state

  • Use when you are storing infrequently changed, global information that is used by many users.
  • It is maintained in below events :
    Application_Start – Code that runs on application startup
    Application_End – Code that runs on application shutdown
    Application_Error – Code that runs when an unhandled error occurs
    Session_Start – Code that runs when a new session is started
    Session_End – Code that runs when a session ends.

Session state

  • Use when you are storing short-lived information that is specific to an individual session
  • There are four modes for session state :
    In Proc mode – It is default mode and is used to store data on the web server.
    State Server mode – It is used to store data in the Asp.Net State service.  Session state does not lost when web application restarted and can be shared between multiple web servers in the web farming.
    SQL Server mode – It is used to store data in the SQL Server.  Session state does not lost when web application restarted and can be shared between multiple web servers in the web farming.
    Custom mode – It is used to store data in custome location.

Profile properties
Use when you are storing user-specific information that needs to be persisted after the user session is expired and needs to be retrieved again on subsequent visits to your application.

Database support
Use when you are storing large amounts of information, managing transactions, or the information must survive application and session restarts.