locked
store a class object in a view state RRS feed

  • Question

  • User370347305 posted

    Hi, I have a created a class object. If I want to store in the view state, do I need to serialize it ?

    thanks. !

    Wednesday, September 28, 2011 11:29 AM

Answers

  • User-578657687 posted

    Hi,

    Please let me know if there any way to save the information across multiple postbacks in the same page apart from ViewState technique

    ASP.NET provide multiple options that help you to preserve the data after postback:

    • View state

    • Control state

    • Hidden fields

    • Cookies

    • Query strings

    • Application state

    • Session state

    More inforamtion about state management please check this article:

    http://msdn.microsoft.com/en-us/library/75x4ha6s.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 30, 2011 3:41 AM
  • User-1856974186 posted

    Well, that does work, but it's not what ViewState was intended for. ViewState is meant to preserve the contents of the controls across postbacks, even though you can use it to store other data. The real problem is the size of the page; the more you store in ViewState the larger your page, which means increased transfer and rendering times. If your application is internal (ie on a corporate LAN) then you may find that's an acceptable solution. For an internet application, where internet speeds vary, anything you can do to reduce the size of the page is a good thing.

    Ultimately, ViewState does work and yes you can use it. Personally I try and keep my pages as small as possible.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 4, 2011 5:03 AM

All replies

  • User-1516073966 posted

    Hi,

    Yes, You have to serialize the object and then save it to the view state.

    Add the Serializable attribute to the class.

    Wednesday, September 28, 2011 11:57 AM
  • User370347305 posted

    Hi, Please let me know if there any way to save the information across multiple postbacks in the same page apart from ViewState technique.

    thanks. !

    Wednesday, September 28, 2011 12:06 PM
  • User-2103114970 posted

    use [Serializable] atribute top of your class

    so you use viewstate object in viewstate.

    [Serializable]
    public class A

    {

    }

    A objA=new A();

        A objA=new A();
            ViewState["objAVal"] = objA;

    Wednesday, September 28, 2011 2:19 PM
  • User-578657687 posted

    Hi,

    Please let me know if there any way to save the information across multiple postbacks in the same page apart from ViewState technique

    ASP.NET provide multiple options that help you to preserve the data after postback:

    • View state

    • Control state

    • Hidden fields

    • Cookies

    • Query strings

    • Application state

    • Session state

    More inforamtion about state management please check this article:

    http://msdn.microsoft.com/en-us/library/75x4ha6s.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 30, 2011 3:41 AM
  • User-1856974186 posted

    Just because you can, doesn't mean you should. ViewState really isn't the best place for class storage, especially if the class has a number of properties. ViewState is transferred to and from the client browser on every request (including postbacks), so you're increasing the page size and adding a performance drop in doing so.

    For one or two properties this might be acceptable (depending upon your application), but consider other solutions.

    Monday, October 3, 2011 7:16 AM
  • User370347305 posted

    Hi all,

    thanks for your comments !!

     @ Dave, I got 15-20 fields in my class. So which technique suits for me.

    Monday, October 3, 2011 7:19 AM
  • User-1856974186 posted

    It really depends upon why you're storing the data.

    Personally, for 15-20 fields I wouldn't use ViewState; I'd probably use Session.

    Tuesday, October 4, 2011 3:36 AM
  • User370347305 posted

    @ Dave, I have used View State since I read like this - Viewstate when we have multiple postbacks in one page. Session state or session variables are used when we want to access data across multiple pages.

    Tuesday, October 4, 2011 4:58 AM
  • User-1856974186 posted

    Well, that does work, but it's not what ViewState was intended for. ViewState is meant to preserve the contents of the controls across postbacks, even though you can use it to store other data. The real problem is the size of the page; the more you store in ViewState the larger your page, which means increased transfer and rendering times. If your application is internal (ie on a corporate LAN) then you may find that's an acceptable solution. For an internet application, where internet speeds vary, anything you can do to reduce the size of the page is a good thing.

    Ultimately, ViewState does work and yes you can use it. Personally I try and keep my pages as small as possible.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 4, 2011 5:03 AM