0

I am still a beginner and I can't figure out the difference between Session and ViewState.

For example, when posting back a page, which one should we use to store our variables, Session or ViewState? Why?

William
  • 332
  • 2
  • 10
  • 21
  • @Dalorzo yes that's right sorry about that – William Feb 14 '15 at 04:13
  • 1
    Session is stored in memory on the server and is meant to be used across pages. ViewState stores your data in a hidden form field that is used to maintain the data across postbacks within a single page. Session can be very brittle since it will be lost when the application pool recycles or if the user hasn't done anything for a while (usually 20 minutes). Session has long been considered not the best solution, though it's good when used properly and with safeguards. – Mark Fitzpatrick Feb 14 '15 at 05:09

2 Answers2

2

1) Session and viewstate are very opposite based on its stored value.. Session stores value at the server side and viewState stores value at clientside in page as a
hidden field (you can see by view pagesource option).

2) Viewstate can not navigate stored value by it's post backing page(or redirecting page), but session can share values between all the pages in the application scope.

Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62
1

Depend on your requirement to access those values on postback, you can store variable either in session or in viewstate. If you do not want to access value of variable in other pages, then you can use viewstate, else session.

Mukund Thakkar
  • 1,225
  • 14
  • 19