Why ViewState/ControlState Loaded After CreateChildControls?

Discussion in 'ASP .Net' started by Mark Olbert, Jan 31, 2007.

  1. Mark Olbert

    Mark Olbert Guest

    Tonight's episode of me beating my head bloody against the monitor was the result of ViewState/ControlState information not being
    available when CreateChildControls() gets called for a CompositeControl.

    So, my question is, >>why<< isn't ViewState/ControlState available before CreateChildControls() gets called by the Framework? Is it
    just me, or does it seem really counterintuitive to be creating child controls on postback without the previously-stored state
    information being available? Is it that ViewState/ControlState are associated with the controls themselves, so before the controls
    are created the state data can't be made available?

    - Mark
    Mark Olbert, Jan 31, 2007
    #1
    1. Advertising

  2. Mark Olbert

    Ben Rush Guest

    Are you dynamically adding the composite control to the page?

    --
    ~~~~~~~~~~~
    Ben Rush
    http://www.ben-rush.net/compilererrors


    "Mark Olbert" <> wrote in message
    news:...
    > Tonight's episode of me beating my head bloody against the monitor was the
    > result of ViewState/ControlState information not being
    > available when CreateChildControls() gets called for a CompositeControl.
    >
    > So, my question is, >>why<< isn't ViewState/ControlState available before
    > CreateChildControls() gets called by the Framework? Is it
    > just me, or does it seem really counterintuitive to be creating child
    > controls on postback without the previously-stored state
    > information being available? Is it that ViewState/ControlState are
    > associated with the controls themselves, so before the controls
    > are created the state data can't be made available?
    >
    > - Mark
    Ben Rush, Jan 31, 2007
    #2
    1. Advertising

  3. Mark,

    I think it makes perfect sense. ViewState contains values that are to be
    assigned to control properties. Obviously, the controls are supposed to be
    created before this. Read more about the page life cycle:

    http://msdn2.microsoft.com/en-us/library/ms178472(VS.80).aspx

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "Mark Olbert" <> wrote in message
    news:...
    > Tonight's episode of me beating my head bloody against the monitor was the
    > result of ViewState/ControlState information not being
    > available when CreateChildControls() gets called for a CompositeControl.
    >
    > So, my question is, >>why<< isn't ViewState/ControlState available before
    > CreateChildControls() gets called by the Framework? Is it
    > just me, or does it seem really counterintuitive to be creating child
    > controls on postback without the previously-stored state
    > information being available? Is it that ViewState/ControlState are
    > associated with the controls themselves, so before the controls
    > are created the state data can't be made available?
    >
    > - Mark
    Eliyahu Goldin, Jan 31, 2007
    #3
  4. Thanks for Eliyahu's input,

    Hi Mark,

    As Eliyahu has mentioned, the restore of controlstate/viewstate does occur
    after the control's "CreatechildControls" be called. Actually, except the
    first request(before postback), in each postback request, the control will
    first reconstruct its control Tree by calling "CreateChildControls" method.
    Then, it perform Init on the control hierarchy and restore
    viewstate/control state from the persistent storage(the <input hidden >
    element in page). And In Load event, the ViewState should has been
    available there. So generally, in "CreateChildControls" method, we should
    only put code that simply construct the control tree of webcontrol, and the
    code logic there should not rely on those resource that are not available
    at that time(such as ViewState).

    I think you may have some code logic about the control structure(sub
    control) depend on the viewstate. Would you provide some further
    information on this? We can try looking for some other approaches to
    overcome the issue.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead



    ==================================================

    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.



    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.

    ==================================================



    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], Jan 31, 2007
    #4
  5. Mark Olbert

    Mark Olbert Guest

    Me, I tend to like to have all my information and "stuff" available before building things...whether it's a woodworking project,
    cooking a meal, or creating custom controls :). Just a personal quirk, I guess, and it's why I find it so counter-intuitive that
    ViewState/ControlState info isn't available until after I build a constituent control.

    Thanks for the reference.

    - Mark

    On Wed, 31 Jan 2007 10:18:42 +0200, "Eliyahu Goldin" <> wrote:

    >Mark,
    >
    >I think it makes perfect sense. ViewState contains values that are to be
    >assigned to control properties. Obviously, the controls are supposed to be
    >created before this. Read more about the page life cycle:
    >
    >http://msdn2.microsoft.com/en-us/library/ms178472(VS.80).aspx
    Mark Olbert, Jan 31, 2007
    #5
  6. I agree that it's necessary to have all the information and stuff available
    before building since that can avoid running into a dead way. Also,
    sometimes those programming model like the ASP.NET control lifecycle look
    like paradox, however, after we inspect it detailedly, we would find it
    quite straighforward to use. Anyway, please feel free to post here if you
    meet any other problems or have any questions on this.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], Feb 1, 2007
    #6
    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. =?Utf-8?B?UGF2YW4=?=
    Replies:
    0
    Views:
    472
    =?Utf-8?B?UGF2YW4=?=
    Dec 7, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,804
    Smokey Grindel
    Dec 2, 2006
  3. bill
    Replies:
    1
    Views:
    487
    news.microsoft.com
    Dec 7, 2007
  4. Edward
    Replies:
    2
    Views:
    594
    Edward
    May 12, 2008
  5. Craig

    CreateChildControls and Exception Failed to Load ViewState

    Craig, Feb 15, 2005, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    193
    Craig
    Feb 16, 2005
Loading...

Share This Page