What is the difference between SessionState and ViewState?
Asked Answered
U

6

119

What is the difference between SessionState and ViewState in ASP.NET?

Unprofessional answered 9/4, 2009 at 9:24 Comment(1)
I tried Google, but I could not find an overview.Unprofessional
N
146

Session State contains information that is pertaining to a specific session (by a particular client/browser/machine) with the server. It's a way to track what the user is doing on the site.. across multiple pages...amid the statelessness of the Web. e.g. the contents of a particular user's shopping cart is session data. Cookies can be used for session state.
View State on the other hand is information specific to particular web page. It is stored in a hidden field so that it isn't visible to the user. It is used to maintain the user's illusion that the page remembers what he did on it the last time - dont give him a clean page every time he posts back. Check this page for more.

Nahamas answered 9/4, 2009 at 9:32 Comment(0)
N
97

Session state is saved on the server, ViewState is saved in the page.

Session state is usually cleared after a period of inactivity from the user (no request happened containing the session id in the request cookies).

The view state is posted on subsequent post back in a hidden field.

Nitrile answered 9/4, 2009 at 9:27 Comment(2)
+1 Yes - important point that the ViewState travels up and down between client and server, but SessionState stays on the server.Surrounding
This should have been the official answerTube
L
33

SessionState

  • Can be persisted in memory, which makes it a fast solution. Which means state cannot be shared in the Web Farm/Web Garden.
  • Can be persisted in a Database, useful for Web Farms / Web Gardens.
  • Is Cleared when the session dies - usually after 20min of inactivity.

ViewState

  • Is sent back and forth between the server and client, taking up bandwidth.
  • Has no expiration date.
  • Is useful in a Web Farm / Web Garden
Lowelllowenstein answered 9/4, 2009 at 9:27 Comment(0)
H
14

Usage: If you're going to store information that you want to access on different web pages, you can use SessionState

If you want to store information that you want to access from the same page, then you can use Viewstate

Storage The Viewstate is stored within the page itself (in encrypted text), while the Sessionstate is stored in the server.

The SessionState will clear in the following conditions

  1. Cleared by programmer
  2. Cleared by user
  3. Timeout
Hirai answered 9/4, 2009 at 10:13 Comment(1)
ViewStates are not encrypted. They just look that way, but can be easily decoded.Kremlin
D
5

Session is used mainly for storing user specific data [ session specific data ]. In the case of session you can use the value for the whole session until the session expires or the user abandons the session. Viewstate is the type of data that has scope only in the page in which it is used. You canot have viewstate values accesible to other pages unless you transfer those values to the desired page. Also in the case of viewstate all the server side control datas are transferred to the server as key value pair in __Viewstate and transferred back and rendered to the appropriate control in client when postback occurs.

Deadwood answered 9/4, 2009 at 9:34 Comment(0)
C
0

View State is a technique to maintain the state of controls during page post-back, meaning it stores the page value at the time of post-back (sending and receiving information from the server) of your page and the view state data can be used when the page is posted back to the server and a new instance of the page is created.

View State advantages:

  • Very easy to implement.
  • Stored on the client browser in a hidden field as a form of Base64 Encoding String not encrypted and can be decoded easily.
  • Good with HTTP data transfer

View State disadvantages:

  • The performance overhead for the page is larger data stored in the view state.

  • Stored as encoded and not very safe to use with sensitive information.

Session State is another state management technique to store state, meaning it helps in storing and using values from previous requests. Whenever the user requests a web form from a web application it will get treated as a new request. an ASP.NET session will be used to store the previous requests for a specified time period.

Here are the various session modes available in ASP.NET :

  • Off
  • InProc
  • StateServer
  • SQLServer
  • Custom

Each mode has a different behavior in a web application. They have their own advantages and disadvantages.

You can find a detailed explanation from the below URL:

https://www.c-sharpcorner.com/UploadFile/de41d6/view-state-vs-session-state-vs-application-state/

Chops answered 22/6, 2023 at 23:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.