Help! Serializing Viewstate Problems!?!?

  • Thread starter Thread starter Z D
  • Start date Start date
Z

Z D

Hello,

I thought that viewstate is inherintly serializable because its stored in a
users browser in text (serialized) format??

Anyways - Here's the situation: I have some pages that have many grids on it
and thus my viewstate was getting very large (75kb+) and it was slowing down
the page loads for the end user.

SO - I decided to store the viewstate in session (on the server) instead of
having it written out to the browser. This GREATLY increased performance
and things were just fine!

Then I decided to have the session stored in a state server instead of
InProc on the same web server because eventually I'd want to add an
additional load-balanced webserver.

This is when the trouble began, now, I get the following error on the pages
that I try to save the viewstate to seesion. Note: All other session
varibles seem to work, except when trying to save viewstate to session. I've
posted the error and the relavent code below, pelase help!!


THE ERROR:
==========
Unable to serialize the session state. Please note that non-serializable
objects or MarshalByRef objects are not permitted when session state mode is
'StateServer' or 'SQLServer'.

THE CODE:
=========
'Save Viewstate in session.
Protected Overrides Sub SavePageStateToPersistenceMedium(ByVal viewState As
Object)
Me.Session.Add("PageViewState", viewState)
End Sub

'Retreive viewstate from session
Protected Overrides Function LoadPageStateFromPersistenceMedium() As Object
Return Me.Session("PageViewState")
End Function


If anybody can tell me how this error makes sense and how to avoid it I
would appreciate it very much. I dont understand why its complaining about
viewstate not being serializable because I thought that was implicit since
it's stored in the users browser!

thanks in advance for any help,
-ZD
 
ViewState itself is not serializable. It CONTAINS serializable data. You
would have the same problem if you tried to put non-serializable objects
INTO ViewState.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Hi, ZD,

Try to cast the parameter in SavePageStateToPersistenceMedium to
System.Web.UI.Triplet. You should try to see if this class is marked with
Serializable(true) yourself as I don't have any application that is running
in a StateServer mode to test it.

Hope this helps
Martin
 
Kevin,

Thanks for your reply.

Do you know how I would then go about saving the CONTAINED data into session
and then loading it into viewstate? (instead of saving the actual viewstate
object in session)?

Thanks
-ZD
 
If you're using Session, why bother with ViewState at all?

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Martin,

Thanks for your reply.

As you suggested, I tried casting it to System.Web.UI.Triplet when saving it
to viewstate:

Protected Overrides Sub SavePageStateToPersistenceMedium(ByVal viewState As
Object)
Me.Session.Add("PageViewState", CType(viewState, System.Web.UI.Triplet))
End Sub


I get the same error. I also noticed on the stack trace this:
[SerializationException: The type System.Web.UI.Triplet in Assembly
System.Web, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a is not marked as serializable.]


Do you have any ideas as to how I can save the data in session (using a
state server) so that I don't have to write out all that viewstate to the
browser?

thanks in advance,
-ZD
 
We have some 3rd party controls that write out stuff to viewstate.

But that's not the issue. I'd like to figure out a way to actually store
viewstate in sesion when using a stateserver. It works fine when session is
stored InProc, but as soon as I moved it to StateServer that's when the
problems arose.

Any ideas?

thanks
-ZD
 
you need to use a special serializer for viewstate:

air code:

protected override void SavePageStateToPersistenceMedium( object
viewState )
{
LosFormatter format = new LosFormatter();
StringWriter writer = new StringWriter();
format.Serialize( writer, viewState);
Session[ "ViewStateKey" ] = new StringBuilder( writer.ToString() );
}

protected override object LoadPageStateFromPersistenceMedium()
{
LosFormatter format = new LosFormatter();
return format.Deserialize( Session["ViewStateKey "].ToString() );
}


-- bruce (sqlwork.com)
 
Sure. Just loop through the ViewState and add each imte to Session.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Back
Top