It is known that the web is stateless, which means every time a specific webpage is requested. It is recreated each time and posted to the server. Also, HTTP is a stateless protocol, i.e. it cannot hold client information on the webpage. So it need to maintain the state of a page and the server-side as well, State management is done. ViewState and SessionState are used for client-side state management and server-side state management respectively. The basic difference between these two is that the ViewState is to manage state at the client’s end, making state management easy for end-user while SessionState manages state at the server’s end, making it easy to manage content from this end too.
ViewState: It is maintained at only one level that is page-level. Changes made on a single page is not visible on other pages. Information that is gathered in view state is stored for the clients only and cannot be transferred to any other place. View state is synonymous with serializable data only.
ViewState has a tendency for the persistence of page-instance-specific data. When view state is used, the values posted of a particular page persist in the browse area that the client is using and post back only when the entire operation is done. The data of the previous page is no longer available when another page is loaded. Also, Data is not secure in this case because it is exposed to clients. Encryption can be used for data security.
SessionState: It is maintained at session-level and data can be accessed across all pages in the web application. The information is stored within the server and can be accessed by any person that has access to the server where the information is stored.
SessionState has the tendency for the persistence of user-specific data and is maintained on the server-side. This data remains available until the time that the session is completed or the browser is closed by the user. The session state is only valid for type objects.
Differences between ViewState and SessionState:
ViewState | SessionState |
---|---|
Maintained at page level only. | Maintained at session level. |
View state can only be visible from a single page and not multiple pages. | Session state value availability is across all pages available in a user session. |
It will retain values in the event of a postback operation occurring. | In session state, user data remains in the server. Data is available to user until the browser is closed or there is session expiration. |
Information is stored on the client’s end only. | Information is stored on the server. |
used to allow the persistence of page-instance-specific data. | used for the persistence of user-specific data on the server’s end. |
ViewState values are lost/cleared when new page is loaded. | SessionState can be cleared by programmer or user or in case of timeouts. |
Usage:
- SessionState: It can be used to store information that you wish to access on different web pages.
- ViewState It can be used to store information that you wish to access from same web page.