April 10, 2011

State Management in ASP.NET

Description:
    This is the process of maintaining the state of the page information over multiple requests. As we know web pages developed in ASP.NET uses HTTP protocol which is state less. It means each web request is treated as a new request at the web server end. So to build an interactive web application it is very much necessary  to keep the state of the page and user information.
    Depending upon the storage location and process of storage state management is divided in to two categories. 
                        1. Client side state management.
                        2. Server side state management.


Client side state management:
    Client side state management involves storing information on the client side inside the pages or on the client machine. The different types of client side state management options available in ASP.NET are
                                     1. View State
                                     2. Control State
                                     3. Cookies
                                     4. Query String
                                     5. Hidden Field


View State: 
    View State is the technique used by ASP.NET to keep the state of the page across post backs. This section is explained on this blog What is view state and its advantages and disadvantages.


Control State:
    This is almost similar to the view state but its functionality is independent of view state,    it cannot be turned off like we did in view state. This state is mainly designed to keep the essential information of the custom controls. The information of this state are stored in the same hidden field that is used to store the view state data. 


Hidden Fields:
    Hidden fields  provides a way to store information without displaying. It stores a single value in its value field and stores information in HTML format inside the page. This is not a secure way as it can be easily tampered.


Cookies:
    Cookies are basically designed to store small amount of information  that frequently changes. This data are always sent with the request to the web server. Cookies are stored as a text file in the clients file system (persistent cookie) or in memory in the client browser session (temporary cookie). And ASP.NET allow users to set the expiration policy on them. The main disadvantage of using cookie are the size limitation (4096 bytes, new versions of browser now starting supports of 8MB size) which is restricted by most of the browsers. And the other one is user have the option to disallowing cookies on their machine, in this situation cookies have no use.


Query String:
    This is the process of sending information on the URL. The query string data is appended at the end of the URL. This is basically used to submit information back to the current page or to another page. Similar to the cookies query string have also a size limitation on its length and another one is it is not much secure as it is visible in the URL.


Server side state management:
    Server side state management involves storing data at the server end. This technique has high security compared to the client side state management options. But the main disadvantage is that it uses server resource to store information, increase in storage data volume affects the server performance.
    The different server side state management options available in ASP.NET are.
                           1. Application State
                           2. Session State
                           3. Profile Properties
                           4. Database Supports


Application State:
     An application state is basically used to store global application specific information. These are available by the entire application and not user specific like the session data. ASP.NET provides Application state via the HttpApplicationState class.


Session State:
    Session state is used to store the user specific data in the server. It is described in details in the article "What is a session in ASP.NET". 


Profile Properties:
    Like the session state profile properties provide the functionality to store user specific  information. But the difference between them are session data expires with the expiration of the session or on the restart of the application and server while the profile data is never lost, even if on the restarts of application and server. ASP.NET profiler allows to easily mange user information without requiring you to create and maintain your own database. 


Database Support:
    This is in use when you require to persist large amount of information which need to be preserved regardless of application  and server restarts.

No comments:

Post a Comment

Your comments are valuable.