saving class to viewstate

A

Angel

I created a web control that accesses a class that performs certain functions. I wanted it saved in viewstate so i do not have to reload the data that is in it everytime i re render the control. But when i save the class to viewstate i get an error that it not serialized. i went back to the class and put in front of the class declaration <serializable()>. i recompiled it but still get the same error that the class cannot be saved because its not serialized...

anybody knows what i can do here. i tried creating a class and performing the viewstate functions manually by overiding the viewstate events and saving my data in a statebag to no avail.

i would like to know why if i specified a class as serializable and still get the error?

thanks
 
J

John Saunders

Angel said:
I created a web control that accesses a class that performs certain
functions. I wanted it saved in viewstate so i do not have to reload the
data that is in it everytime i re render the control. But when i save the
class to viewstate i get an error that it not serialized. i went back to the
class and put in front of the class declaration <serializable()>. i
recompiled it but still get the same error that the class cannot be saved
because its not serialized...
anybody knows what i can do here. i tried creating a class and performing
the viewstate functions manually by overiding the viewstate events and
saving my data in a statebag to no avail.
i would like to know why if i specified a class as serializable and still
get the error?

Maybe your class is serializable, but a member of your class is not.
 
I

Iain

Have you considered using the Session object?

All you need to do then is Session["MyClass"] = MyClass and get it back in
the OnLoad.

This has the benefit of reducing the size of the WEb Page, but the potential
drawback of using memory in the Server.

Iain
Angel said:
I created a web control that accesses a class that performs certain
functions. I wanted it saved in viewstate so i do not have to reload the
data that is in it everytime i re render the control. But when i save the
class to viewstate i get an error that it not serialized. i went back to the
class and put in front of the class declaration <serializable()>. i
recompiled it but still get the same error that the class cannot be saved
because its not serialized...
anybody knows what i can do here. i tried creating a class and performing
the viewstate functions manually by overiding the viewstate events and
saving my data in a statebag to no avail.
 
J

John Saunders

Angel said:
what do you mean one of the members is not? do you mean i need to put
serializable on every method and property in the class?

Some classes are not serializable, as you've discovered. If your class had a
member of that type, then your class would not be serializable, even though
you put "[Serializable]" at tht top.
 
V

Victor Garcia Aprea [MVP]

Angel,

Take a look here[1] to learn about the approach the viewstate serializer
uses when trying to serialize a custom type. You really want to know about
this to keep the viewstate paidload as small as possible,

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx


Angel said:
what do you mean one of the members is not? do you mean i need to put
serializable on every method and property in the class?
 
V

Victor Garcia Aprea [MVP]

oopss... [1] is
http://weblogs.asp.net/vga/archive/2004/05/11/DontLetTheBinaryFormatterGetAtIt.aspx


Victor Garcia Aprea said:
Angel,

Take a look here[1] to learn about the approach the viewstate serializer
uses when trying to serialize a custom type. You really want to know about
this to keep the viewstate paidload as small as possible,

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx


Angel said:
what do you mean one of the members is not? do you mean i need to put
serializable on every method and property in the class? to
 
F

Fritz

Iain said:
Have you considered using the Session object?

All you need to do then is Session["MyClass"] = MyClass and get it back in
the OnLoad.

This has the benefit of reducing the size of the WEb Page, but the
potential drawback of using memory in the Server.

Iain
Angel said:
I created a web control that accesses a class that performs certain
functions. I wanted it saved in viewstate so i do not have to reload the
data that is in it everytime i re render the control. But when i save the
class to viewstate i get an error that it not serialized. i went back to
the class and put in front of the class declaration <serializable()>. i
recompiled it but still get the same error that the class cannot be saved
because its not serialized...
anybody knows what i can do here. i tried creating a class and performing
the viewstate functions manually by overiding the viewstate events and
saving my data in a statebag to no avail.
i would like to know why if i specified a class as serializable and still get the error?

thanks
you should get the same effect with ViewState and it is safer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top