Tuesday 11 November 2014

Difference Between ViewState and Session in Asp.Net

"View State and Session "
  The Two main State Management things used in Asp.net Web applications . We cannot even think of an application without ViewState and Session . So today i decided to gave an overview of what is the difference between viewstate and session why these two terms exist and what are their usage To clearify this difference i am giving some points below

1. So First of all what is a Session ? -- A Session is a Time or a span of time upto which user used that webapplication or for how much session of how much period of time user has used your webapplication will be called session . Session contains some values that will be used throughout the user interaction with the webapplication and used in more than one webpage that user navigate So we can say that it is a variable in simple sense that takes our value from one page to another and Session got distroyed or killed automatically after a particular span or time or it is automatically destroyed or cleared when like user signout of application or when user close the browser

A live example would be a web application where a user login . Each user that logs into your system will have a unique session. We can hold additional data into the session which can be used as the user browses your site. We can hold the username, userrole, userpermissions simply by adding it to the session:


Session[“userrole”] = “programmer”; 
Session[“username”] = “Ageek”; 
Session[“userpermissions"] = “cangotohell”; 

A Session  stores the information on the server side  . It stores the values or it uses server memory for storage of data that it holds in . Session variables will use different new and unique session variables for each different user that logs in to web application . large amount of data can be stored on the session, web sites that have a large amount of traffic not use this method, because it will put a severe load on the server memory.

2. Now What is ViewState ? -- so we are clear with session now comes the turn of viewstate . So ViewState is also used for holding our important data But in case of viewstate data is kept in the single viewstate page . It means viewstate data is not taken from one page to another page in this point it is different from session . 

ViewState is only used to Track to analyse the changes that occur to current page during postbacks of data to pages .The viewstate of page can be viewed if you right click on webpage and then clickon view page source then you will see some code of lines similar to below code --

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value=””></input>

If you are having large number of viewstate fields then it will make the postbacks very slow as all viewstate values will be needed to updated during postbacks so it will make the webpage open slower

Data can be written into viewstate like we do in sessions . But it can retain that data only with life cycle of page after that it will be changed or updated .If you closes that page or redirects to another page then viewstate data will be reset .

How to fill data in viewstate and how to access that data 
ViewState[“PreviousPage”] = “http://www.google.com”; 

Retrieving data form ViewState:
string PreviousPage = ViewState[“PreviousPage”].ToString();  

Share this

0 Comment to "Difference Between ViewState and Session in Asp.Net"

Post a Comment