Thursday, November 4, 2010


This error comes when you are trying to save some object in ViewState. The solution is fairly easy and straight forward.
Solution: Mark the class Serializable, the object of which you want to store in ViewState, for instance you want to save the object of the class TestClass in ViewState. So inorder to get this done you need to marked it as Serializable. Following is the code snippet.
public class TestClass
But why do you need to mark the class/ type as Serializable, did you ever thought about it. In order to understand this we need to know what Serialization is.
Serialization is the process of changing data (object) representation to binary or XLM.
But still the questions are there, why I need to serialize data in order to save it in ViewState. For this we need to understand when we do serialization.
Whenever we need to send data (object) on wire (means to some other client) we does serialization, because data can only be sent in binary or xml (using soap protocol).
OK! I understand that serialization is used when we need to send data (object) over the wire. But still picture is not clear, what does saving data in ViewState have to do with sending data over the wire.
Actually when we need to store data in ViewState we write following code:
ViewState["Key"] = MyObject;
Right! This is server side code written C#, it runs on server not on the client and ViewState is maintained at client side in a hidden field __VIEWSTATE.
Now we server tries to save data in ViewState (means it will send that data to the client) what happens at backend is server need to send that data back to the client and because of the fact that ViewState is maintained at client side class need to be Serializeable.
Hope this post will help you. Have a nice day :).

No comments:

Post a Comment