Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Posted
You can store any object in viewstate that is serializeable (easy saying, that can be stored as string ;-).

A man and a dog have an average of three legs.

Beaware of Statistics.

  • *Experts*
Posted

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.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted (edited)

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.

Edited by hrabia

A man and a dog have an average of three legs.

Beaware of Statistics.

Posted
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.

A man and a dog have an average of three legs.

Beaware of Statistics.

Posted
Than you have to save your object in ViewState, and ViewState on server. Second way is to save your serialized object in database.

A man and a dog have an average of three legs.

Beaware of Statistics.

Posted

No, can't 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.... :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...