Wednesday, April 24, 2013

SerializationException: VewState

In order to save temporary data within a .aspx page, ViewState is my choice.  The data is a List of custom object.
ViewState["TempData"] = new List<ABC>();
Unfortunately, it throws an SerizationException. This error message shows that the class is not serializable. After I add "[Serializable] "  to the class, the problem is solved. But, I don't know why the class needs to be serialized. 

This issue is related to how ViewState packs data into a hidden field. ViewState serialized data into based 64 string and then stored into hidden form field. Later, page can get back state data by deserialized the hidden field.


Understanding ASP.NET View State
[ASP.NET] 利用ViewState保存自訂物件

No comments:

Post a Comment