Simple problem: Viewstate

bungpeng

Well-known member
Joined
Sep 10, 2002
Messages
906
Location
Malaysia
Can I use ViewState to store my object? for example, collection object? like what we can do using "Session".

I was encounter problem when I use Viewstate in this case.

As what I know: limitation of Session is use more server resource and timeout limited. Because I just need to keep my values in the same page, so I think about Viewstate.
 
If I understand correctly, you can serialize an object, then you
can encode it in a string and store it in a ViewState variable, then
decode and then deserialize the string to get the object back.
 
Is it? May I know how to Serialize an object? encode it in a string? and decode and deserialize? I have no idea about that...

Thank you.
 
Ok, your object (class) has to be set with <Serializable> attribute.
Than you have to use BinaryFormater (System.Runtime.Serialization.Formatters.Binary) to serialize your object to stream (MemoryStream or whatever). Stream can be converter to byte array (ToArray) which can be saved in ViewState.
Deserialization is also simple. You can read your array from ViewState, make from it MemoryStream and with BinaryFormater call Deserialize on that stream. The return object must be only casted to your class.
 
Last edited by a moderator:
It is not practicalable because in standard way the ViewState flies between server and client (so has impact on performace). When you need to save your object use Session.
 
Than you have to save your object in ViewState, and ViewState on server. Second way is to save your serialized object in database.
 
No, cant use Application because it is shared by all users...

I think maybe the best way is, try not to use object.... convert my object to string, then I can either use Viewstate or Cookies....

Anyway, TQ for you guys replied.... :)
 
Back
Top