Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Serializing object for viewstate

Status
Not open for further replies.

crazyboybert

Programmer
Jun 27, 2001
798
GB
Hi All

I have a class which implements ISerializable and has the appropriate constructor and overridden GetObjectData methods.

I want to serialize an instance of this class and add it to the pages viewstate. I can successfully serialize the object and add it to a FileStream using a BinaryFormatter but am struggling getting it into ViewState and can't find much out there on this subject. Seems a fairly simple requirement and I think there must be an easy way to do this I'm missing.

Any ideas?

Cheers, Rob.

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
If it properly implements ISerializable, then can you not just:

ViewState.Add("myVar", myClass);

??

-p

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Hmm i thought so too. However when I do this it seems to run fine until postback when I get a HttpException

"The viewstate is invalid for this page and might be corrupted"

The exception is thrown before any attempts to access the serialized object in viewstate.

I'm using session at the moment :-( as I can't afford the time to mess around with it right now, but would much prefer to use the viewstate as the object only has couple of string properties and would be no significant overhead on the page download.

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
That error probably isn't coming from your serializable object.

Try something, though. To make sure that it's properly implementing ISerializable, set the project to use StateServer as its session store. Then, if you're still able to add your object to session, then you know that it's fine.

Here's some info about that error about corrupt ViewState.


Good luck.

-p

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top