Composite Control - Viewstate management for complex properties

Discussion in 'ASP .Net Building Controls' started by Matt T., Sep 7, 2004.

  1. Matt T.

    Matt T. Guest

    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;
    Matt T., Sep 7, 2004
    #1
    1. Advertising

  2. Matt T.

    Teemu Keiski Guest

    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;
    }
    }

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist
    http://blogs.aspadvice.com/joteke



    "Matt T." <> wrote in message
    news:...
    > 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;
    Teemu Keiski, Sep 11, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    2
    Views:
    1,263
    Patrice
    Apr 9, 2010
  2. Chad
    Replies:
    0
    Views:
    221
  3. Paul Yanzick
    Replies:
    4
    Views:
    501
    Steven Cheng[MSFT]
    Jan 9, 2006
  4. Gentian Hila

    complex properties (font) on composite controls

    Gentian Hila, Jan 5, 2006, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    125
    Ken Baltrinic
    Jan 19, 2006
  5. whoisnidhee

    ViewState of the Composite Control composed of custom control

    whoisnidhee, Jul 14, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    316
    Nidhee
    Jul 16, 2004
Loading...

Share This Page