must be marked as Serializable or have a TypeConverter other than ReferenceConverter to be put in vi

  • Thread starter Thread starter Harshdeep Mehta
  • Start date Start date
H

Harshdeep Mehta

Hi all,

I have written a page in which I want to preserve value of object - suppose
A - value, so I used viewstate. but that give me error.

Code :
ViewState["ObjValue"] = objClassA; // Before PostBack
objClassA = (ClassA) ViewState["ObjValue"] // After PostBack

Any Body can help me out?

Thanks in advance.
 
Hi,

as the error tells you, to put a class to ViewState it needs to be
serializable.

It means the object needs to have Serializable attribute so that Framework
serializes it automatically. If you want to customize this serialization
process to ease the task and make it explicit), you can additionally
implement ISerializable interface (means Serializable attribute must still
exist).

But with objects added to ViewState, it is recommended to implement a type
converter. If object has a type converter, it will be used to convert the
object to its string representation. Type converters are helper objects
which are used to object-to-string and string-to-object conversions. Using a
type converter gives the best possible performance (or in simple scenario
just convert the object to and from string manually).

here is how to develop a type converter:
http://msdn.microsoft.com/library/d...guide/html/cpconImplementingTypeConverter.asp
 
Back
Top