How to bring multiple properties between postbacks?

D

Davids

in my small hangman web project I have a Hangman class which has some
methods and properties (int GuessAttempts, string[] GuessedLetters etc..)
which change as the game goes on... Best of all would of course to be able
to save the whole class and then load it up again after postback. I
understand this is not possible, is that true?

In that case I will have to do something like load the class properties into
an array for example and store in the ViewState... or is there any other
more clever way? I know these are not many values to store but this is a
learning-case for me and I foresee in the future I might have much bigger
classes which I'd want to bring in "whole piece" between postbacks!
 
G

Guest

David, you can save the whole object in viewstate, session, etc. if that's
what you want to do (e.g. viewstate.add("MyKey", myObject)), then cast it
back to your object type when you pull it back out. This should work for
anything that is "serializable".

Bill
 
D

Davids

hmm thx - thought I'd read somewhere that a whole object/class couldn't be
serialised... must have misunderstood that. So what then are the limits for
serialising (if there are any!!)? When do I know when I'm putting a too big
object into the viewstate?

Bill Borg said:
David, you can save the whole object in viewstate, session, etc. if that's
what you want to do (e.g. viewstate.add("MyKey", myObject)), then cast it
back to your object type when you pull it back out. This should work for
anything that is "serializable".

Bill

Davids said:
in my small hangman web project I have a Hangman class which has some
methods and properties (int GuessAttempts, string[] GuessedLetters etc..)
which change as the game goes on... Best of all would of course to be
able
to save the whole class and then load it up again after postback. I
understand this is not possible, is that true?

In that case I will have to do something like load the class properties
into
an array for example and store in the ViewState... or is there any other
more clever way? I know these are not many values to store but this is a
learning-case for me and I foresee in the future I might have much bigger
classes which I'd want to bring in "whole piece" between postbacks!
 
G

Guest

Sorry, I'm no expert on serialization...I'd search out on this forum or go to
google. Re "how big" in viewstate, I doubt there's a physical limit (other
than the total size of a request that IIS might allow), so just experiment.
If you build a datagrid with, say, a hundred rows, check out the viewstate
string and you'll see it's *huge*. As big as that text is, I've seen people
shy away from it, but then for some reason they have no qualms about sending
a 50K image down the pipe. One tradeoff between session and viewstate is that
the viewstate data travels the pipe, so the burden is on the user, whereas
the session data is held on the server and therefore the burden is there. Go
to google and you'll find tons on viewstate/session. Have fun.

Davids said:
hmm thx - thought I'd read somewhere that a whole object/class couldn't be
serialised... must have misunderstood that. So what then are the limits for
serialising (if there are any!!)? When do I know when I'm putting a too big
object into the viewstate?

Bill Borg said:
David, you can save the whole object in viewstate, session, etc. if that's
what you want to do (e.g. viewstate.add("MyKey", myObject)), then cast it
back to your object type when you pull it back out. This should work for
anything that is "serializable".

Bill

Davids said:
in my small hangman web project I have a Hangman class which has some
methods and properties (int GuessAttempts, string[] GuessedLetters etc..)
which change as the game goes on... Best of all would of course to be
able
to save the whole class and then load it up again after postback. I
understand this is not possible, is that true?

In that case I will have to do something like load the class properties
into
an array for example and store in the ViewState... or is there any other
more clever way? I know these are not many values to store but this is a
learning-case for me and I foresee in the future I might have much bigger
classes which I'd want to bring in "whole piece" between postbacks!
 
M

Michael Wood

If you use standard .Net serialization only the public properties and fields are serialized, the private fields are not serialized. In order to bring the "whole" class over you would need to implement the ISerializable interface and write your own serialization.

MikeWo
www.mvwood.com/blog

Davids wrote:

nntp://nntp.fuse.net/microsoft.public.dotnet.framework.aspnet/<[email protected]>

hmm thx - thought I'd read somewhere that a whole object/class couldn't be
serialised... must have misunderstood that. So what then are the limits for
serialising (if there are any!!)? When do I know when I'm putting a too big
object into the viewstate?
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top