Session vs ViewState
ViewState and session are two of the solutions for problems in web applications. The problem involves a computer user accessing a web page in a specific period of time. After some time of use, or after the user closed the application, the web page’s information and access is deleted from recent use logs.
ViewState is valid only during postbacks and only applicable to serialized data. ViewState, first and foremost, is object/architecture that enables Web controls to maintain their value between postbacks. Like session, ViewState can run and be accessible to a server.
The information in ViewState is stored for the client only as an encrypted value in the browser’s hidden fields. This makes data unsecured and possibly subject to hacking. During postbacks, ViewState tracks the changes in a particular website.
ViewState also retains the value of control for the particular page. This means that the data on a page isn’t and cannot be available, accessed, or replicated into another page. ViewState is maintained at the page level or current, live page. It has no point of expiration and can be enabled or disabled in ViewState for specific controls.
ViewState can be seen in viewing the source code of the page which lasts until the page is closed. ViewState is mostly used to hold a small amount of data (usually a string and other serialized data) on the current page. Also, ViewState consumes more bandwidth compared to session but less memory and memory space. ViewState doesn’t involve too much memory since all the data is lost the moment the page is closed.
Meanwhile, session is more commonly used for Web pages and affects not only the page but the whole application and entire duration of a user using that particular browser or page. Session is valid for any type of objects and is stored in the server memory of the browser. The data it retains is the data of the computer user or, in other words, user specific data. The data is available until the user closes the browser application or the session expires on its own.
Unlike in ViewState, the data in session can be accessed or replicated into another window or page within the session. Sessions also don’t have the ability to disable or enable specific controls. Session also involves different kinds of data to be stored in its memory receptacle. Session also has a bigger memory compared to ViewState since it uses server memory. The downside of this is that the session has an expiration time, and the quantity of data stored in the server memory can affect the time server load.
1.Aside from ViewState and session, other ASP.NET solutions include application variables, cache, and cookies. 2.ViewState and session can both run and be accessible in a server environment.
3.ViewState is used on the client side while session is used on the server side. Being on the client side makes ViewState have no expiration. The opposite (having an expiration and being on the server side) is for session.
4.ViewState can only hold a string or serializable data while session can hold a large multitude type of data. This makes the amount of data in ViewState much smaller compared to session’s.
5.Session can replicate or access data to a new page or window while ViewState isn’t capable of this feature.
6.Session stores its data in server memory while ViewState hides its data in a browser’s hidden fields as encrypted data. 7.Since the server memory holds different kinds of data, a large amount of stored data can affect server load.