Viewstate and Collections

M

mark

Hi there,

I've got a problem. I need to store my server controls view state.
This all seems very easy when dealing with simple properties, just pop
them in the Viewstate bag and retrieve them when you need them.

Problem for me is that I have a Server Control which is a form of
datagrid display and it looks like the following: -

My Server Control
- ColumnHeaders (collection)
- ColumnHeader (class)
- HeaderButtons (collection)
- Button (class)
- DetailButtons (collection)
- Button (class)
- Rows (collection)
- Row (collection)
- Item (class)
- ID (property
- Title (property)
- Datasource (method)
- Etc...

I've looked all over the net trying to find out how to save the
viewstate of all my collections but I haven't found anything that
helps. I've read about LoadViewState and SaveViewState and the
IStateManager and seen examples using them but I haven't seen any
examples that show the Container, Collection and Class.

I guess that my collection and classes need to implement the
IStateManger interface? Or is it just the collection?

Do I need to use TrackViewState?

I know these could be all very simple questions to answer, but they
aren't for me! :-(

Please help! What would be really useful would be just a short example
in C# showing how it's done. I find that the best way to understand.

Thank you in advance,

Mark (in dire need) Bateman...
 
A

Alessandro Zifiglio

hi mark,
To improve efficiency, or to save a custom type that cannot be stored by
default in ViewState, a control can customize how property data is stored in
ViewState. If a control customizes storage of property data, the control
must also provide a custom implementation for restoring property values from
data stored in ViewState. the base class Control provides two methods for
this purpose, SaveViewState and LoadViewState.
And yes, you need to use trackviewstate. The examples shows everything in
detail, give it a look :
http://msdn.microsoft.com/library/d...html/cpcontemplateddataboundcontrolsample.asp
 
L

Lostinet.Web Support

make your collection implement IStateManager

override TrackViewState,SaveViewState,LoadViewState as:

override protected void TrackViewState()
{
base.TrackViewState();
MyItems.TrackViewState();
}
override protected object SaveViewState()
{
return new Pair(base.SaveViewState(),MyItems.SaveViewState());
}
override protected void LoadViewState(object state)
{
Pair p=(Pair)state;
if(p==null)return;
base.LoadViewState(p.First);
MyItems.SaveViewState(p.Second);
}
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top