I have a simple class with some properties and other data. Untill i stick serializable attribute to the class, i cannot save the object of the class into the viewstate.
Why Viewstate can contain only serializable objects?
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
As the viewstate of a request is passed back to the browser as a serialised representation embedded within the generated page’s HTML it makes sense that only serialisable objects can be placed within it (otherwise it may fail to represent that which it contains.) That viewstate is then de-serialised during the next request.
If you are using POCO’s marking them as serialisable should be trivial enough. There is an excellent resource on understanding how viewstate works, what it is etc. here:
It goes into the full lifecycle of the state and gives detail about much of it’s implementation and use from the prospective of a developer.
Because the ViewState is serialized before it is sent to the Client.
Maybe you can store your data in the Session object instead. It depends on what your class does and how it’s used.
Simply because the data stored in viewstate needs to be serialized (and deserialized).
The viewstate is essentially just text, so anything stored in it needs to be able to be represented as text and to be reconstructed from that serialized text into object form.
I think this summarizes the answer:
The state goes out of control from ther Asp.Net code to the HTTP handler which does not understand your code which will go out of scope, and must serialize it in a datastore (it’s that lot of garbage you see when you view the source of a asp.net page) then give it back when another postback requires it.