ViewState and ViewState object
Hi guys
I fully understand what ViewState does to maintain the values1 of controls in a web form. What I would like to know is the purpose of the Session-like usage as follows -
ViewState["abc"] = "xyz";
x = ViewState["abc"];
Please tell me what is this used for.
Regards
Suhaib
Re: ViewState and ViewState object
Its similar to a key based map. You store the values into the ViewState collection object with a key/identifier and then when you want to retrieve a value out of it provide the corresponding key for it to search and return it to you. By the way when you retrieve the object from the ViewState or even Session you need to cast it to the relevant data type for it to be usable. For more information you could look up with a google search. Hope this helps. :thumb:
Re: ViewState and ViewState object
Quote:
Originally Posted by exterminator
Its similar to a key based map. You store the values into the ViewState collection object with a key/identifier and then when you want to retrieve a value out of it provide the corresponding key for it to search and return it to you. By the way when you retrieve the object from the ViewState or even Session you need to cast it to the relevant data type for it to be usable. For more information you could look up with a google search. Hope this helps. :thumb:
Thanks for replying
So, in that case can ViewState and Session be used interchangeably ??
Or, are there any considerations ??
Regards
Suhaib
Re: ViewState and ViewState object
Nopes. You cannot use them interchangeably. They have very different properties and security levels. Viewstate is related to individual aspx pages whereas the Session is related to a complete session of one user. Apart from that Session variables are stored on the web server running your web application where as the viewstate is stored in a hidden field on the page. So what you want to be stored on the server should not be there with the viewstate field on the aspx page. That would cause the slow rendering of your aspx pages if the objects being stored in viewstate are larger. Better would be let the viewstate just keep the information related to the state of the controls and for all the other objects that you wish to keep alive for a user session in a session state. You could read up more about them on MSDN, some urls that i could find for you:
1. Viewstate
2. Viewstate vs Session state
You can find more with simple googling. Hope this helps. :thumb:
EDIT: Also, viewstate could only be used if your class' object is serializable.