CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2003
    Location
    Bangalore, INDIA
    Posts
    180

    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

  2. #2
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up 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.

  3. #3
    Join Date
    Jan 2003
    Location
    Bangalore, INDIA
    Posts
    180

    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.
    Thanks for replying
    So, in that case can ViewState and Session be used interchangeably ??
    Or, are there any considerations ??

    Regards

    Suhaib

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up 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.

    EDIT: Also, viewstate could only be used if your class' object is serializable.
    Last edited by exterminator; August 31st, 2005 at 04:57 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured