In my previous tutorials, I’d explained the difference between appsettings and connectionstring in asp.net, import excel data into sql server using bulk insert, top 100+ evergreen asp.net interview collection, difference between executereader executenonquery and executescalar and other similar tutorials on difference, sessionstate, viewstate here.
Now here in this tutorial, I’ll explain the main difference between SessionState and ViewState in asp.net.
Difference between SessionState and ViewState
In my opinion, following are few points that technically differentiate session state and view state:
|The view state is maintain in page level||The session state is maintain in session level|
|Scope of a view state is specific to a page only||Scope of a session state is for a user session|
|The ViewState is used to store data that can be used during postbacks for page level||The SessionState is used to store data that can be used anywhere within website or web application in asp.net|
|The view state is a client side state management technique to store data||The session state is a server side state management technique to store data|
|The ViewState of one page is not visible in another page which means when user requests another page, the previous page stored data will be no longer available to use||The SessionState value is available in all pages within a user session which means the data will no longer available if user close the browser or session timeout occurs (usually 20min which is default for session)|
|The ViewState has no specific expiration date or time to wipeout stored data||The SessionState will clear session when the current user close his web browser or session dies due to inactivity of the user within set timeout (default is 20min)|
When to Use SessionState and ViewState?
If you want to access the stored data over website or web application, you should definitely go with session state, whereas if you want to access the stored data for a specific page only (which means persists during postbacks), go with view state.
Which One is More Secure?
SessionState is more secure as compared to ViewState because session state store data value on server side whereas in view state data value store in client side.
I hope by reading the above points, you will get better understanding of the main difference between viewstate and sessionstate in asp.net with its usage and security.
Let me know if you’ve specific questions regarding SessionState and ViewState?
Fairly decent questions. Good Primer for an interview.