Composite Control - Viewstate management for complex properties

M

Matt T.

I have seen several composite control examples where LoadViewState and
SaveViewState are overloaded to track viewstate on complex objects
such as styles, but I cannot find anything that tells me why I need to
do this.

Can someone tell me why is this code not sufficient for Viewstate
management?

if (style == null)
style = new Style();

((IStateManager) style).TrackViewState();

return style;
 
T

Teemu Keiski

Hi,

because in those scenarios when those methods are not overridden, the
container control's ViewState collection is passed as argument to the Style
object (its constructor) which means that the Style automatically has where
to put the state (ViewState collection handles it right away). And note that
this is with the default style of the control (ControlStyle) because the
ViewState collection of the control can be shared with one Style object and
by default it is the ControlStyle/default style.

So, if you have additional style for your control, to save and load it,
you'd need override SaveViewState and LoadViewState (as well as
TrackViewState)

And in case of the Style property, the code should be something like:

public Style MyStyle
{
get
{
if(style==null)
{
style=new Style();
if(IsTrackingViewState)
{
((IStateManager) style).TrackViewState();
}
}
return style;
}
}
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top