Page lifecycle and dynamic controls

Discussion in 'ASP .Net' started by Hans Kesting, Nov 3, 2008.

  1. Hans Kesting

    Hans Kesting Guest

    Hi,

    Is there good information about the asp.net page lifecycle in
    combination with dynamically loaded controls? Or on "how to build
    dynamic controls"? I keep hitting problems where values are not
    available at the moment I need them.

    Current problem:
    In a dynamically loaded ascx there is a dropdown. Based on the selected
    value (reloading an existing value or responding to a
    SelectedIndexChanged event), I create a set of "property edit"
    controls. But when I try to save all selected values, the dropdowns in
    there (my dropdowns, based on existing dropdowns) sometimes come up
    empty.
    This happens especially the first time, so when the property controls
    are built following a SelectedIndexChanged.

    Any suggestions?

    Hans Kesting
    Hans Kesting, Nov 3, 2008
    #1
    1. Advertising

  2. "Hans Kesting" <> a écrit dans le message de
    news: ut$...
    > Hi,
    >
    > Is there good information about the asp.net page lifecycle in combination
    > with dynamically loaded controls? Or on "how to build dynamic controls"? I
    > keep hitting problems where values are not available at the moment I need
    > them.
    >
    > Current problem:
    > In a dynamically loaded ascx there is a dropdown. Based on the selected
    > value (reloading an existing value or responding to a SelectedIndexChanged
    > event), I create a set of "property edit" controls. But when I try to save
    > all selected values, the dropdowns in there (my dropdowns, based on
    > existing dropdowns) sometimes come up empty.
    > This happens especially the first time, so when the property controls are
    > built following a SelectedIndexChanged.
    >
    > Any suggestions?
    >
    > Hans Kesting
    >


    I don't know about you particular problem but you have to recreate everytime
    your dynamic controls in your Page Init function. Still in the init function
    you can set you control's content the first time the page loads
    (!IsPostBack).

    Subsequent page loads, the viewstate will kicks in but still, you have to
    re-create you dynamic control every page loads (even on postbacks).

    Hope it helps
    Mike Gleason jr Couturier, Nov 3, 2008
    #2
    1. Advertising

  3. Hans Kesting

    Hans Kesting Guest

    Mike Gleason jr Couturier was thinking very hard :
    > "Hans Kesting" <> a écrit dans le message de news:
    > ut$...
    >> Hi,
    >>
    >> Is there good information about the asp.net page lifecycle in combination
    >> with dynamically loaded controls? Or on "how to build dynamic controls"? I
    >> keep hitting problems where values are not available at the moment I need
    >> them.
    >>
    >> Current problem:
    >> In a dynamically loaded ascx there is a dropdown. Based on the selected
    >> value (reloading an existing value or responding to a SelectedIndexChanged
    >> event), I create a set of "property edit" controls. But when I try to save
    >> all selected values, the dropdowns in there (my dropdowns, based on
    >> existing dropdowns) sometimes come up empty.
    >> This happens especially the first time, so when the property controls are
    >> built following a SelectedIndexChanged.
    >>
    >> Any suggestions?
    >>
    >> Hans Kesting
    >>

    >
    > I don't know about you particular problem but you have to recreate everytime
    > your dynamic controls in your Page Init function. Still in the init function
    > you can set you control's content the first time the page loads
    > (!IsPostBack).
    >
    > Subsequent page loads, the viewstate will kicks in but still, you have to
    > re-create you dynamic control every page loads (even on postbacks).
    >
    > Hope it helps


    But if I should create everything in Page_Init, how can I respond to an
    IndexChanged event, which doesn't happen until *after* Page_Load?
    I know I should rebuild the controls every time. One of my problems is
    that I the information I need to decide *what* to (re)build isn't
    available yet at the time I should do it.

    And an additional question: does Ajax change that lifecycle? (the
    pulldown and the properties section ar in an UpdatePanel).

    Hans Kesting
    Hans Kesting, Nov 3, 2008
    #3
  4. "Hans Kesting" <> a écrit dans le message de
    news: %...
    > Mike Gleason jr Couturier was thinking very hard :
    >> "Hans Kesting" <> a écrit dans le message de
    >> news: ut$...
    >>> Hi,
    >>>
    >>> Is there good information about the asp.net page lifecycle in
    >>> combination with dynamically loaded controls? Or on "how to build
    >>> dynamic controls"? I keep hitting problems where values are not
    >>> available at the moment I need them.
    >>>
    >>> Current problem:
    >>> In a dynamically loaded ascx there is a dropdown. Based on the selected
    >>> value (reloading an existing value or responding to a
    >>> SelectedIndexChanged event), I create a set of "property edit" controls.
    >>> But when I try to save all selected values, the dropdowns in there (my
    >>> dropdowns, based on existing dropdowns) sometimes come up empty.
    >>> This happens especially the first time, so when the property controls
    >>> are built following a SelectedIndexChanged.
    >>>
    >>> Any suggestions?
    >>>
    >>> Hans Kesting
    >>>

    >>
    >> I don't know about you particular problem but you have to recreate
    >> everytime your dynamic controls in your Page Init function. Still in the
    >> init function you can set you control's content the first time the page
    >> loads (!IsPostBack).
    >>
    >> Subsequent page loads, the viewstate will kicks in but still, you have to
    >> re-create you dynamic control every page loads (even on postbacks).
    >>
    >> Hope it helps

    >
    > But if I should create everything in Page_Init, how can I respond to an
    > IndexChanged event, which doesn't happen until *after* Page_Load?
    > I know I should rebuild the controls every time. One of my problems is
    > that I the information I need to decide *what* to (re)build isn't
    > available yet at the time I should do it.
    >
    > And an additional question: does Ajax change that lifecycle? (the pulldown
    > and the properties section ar in an UpdatePanel).
    >
    > Hans Kesting
    >
    >


    I see.. for the event part you can (after creating it dynamically):

    MyDynamicControl.OnIndexChanged += new [Handler]

    Mike
    Mike Gleason jr Couturier, Nov 3, 2008
    #4
  5. Hans Kesting

    Hans Kesting Guest

    Mike Gleason jr Couturier presented the following explanation :
    > "Hans Kesting" <> a écrit dans le message de news:
    > %...
    >> Mike Gleason jr Couturier was thinking very hard :
    >>> "Hans Kesting" <> a écrit dans le message de
    >>> news: ut$...
    >>>> Hi,
    >>>>
    >>>> Is there good information about the asp.net page lifecycle in combination
    >>>> with dynamically loaded controls? Or on "how to build dynamic controls"?
    >>>> I keep hitting problems where values are not available at the moment I
    >>>> need them.
    >>>>
    >>>> Current problem:
    >>>> In a dynamically loaded ascx there is a dropdown. Based on the selected
    >>>> value (reloading an existing value or responding to a
    >>>> SelectedIndexChanged event), I create a set of "property edit" controls.
    >>>> But when I try to save all selected values, the dropdowns in there (my
    >>>> dropdowns, based on existing dropdowns) sometimes come up empty.
    >>>> This happens especially the first time, so when the property controls are
    >>>> built following a SelectedIndexChanged.
    >>>>
    >>>> Any suggestions?
    >>>>
    >>>> Hans Kesting
    >>>>
    >>>
    >>> I don't know about you particular problem but you have to recreate
    >>> everytime your dynamic controls in your Page Init function. Still in the
    >>> init function you can set you control's content the first time the page
    >>> loads (!IsPostBack).
    >>>
    >>> Subsequent page loads, the viewstate will kicks in but still, you have to
    >>> re-create you dynamic control every page loads (even on postbacks).
    >>>
    >>> Hope it helps

    >>
    >> But if I should create everything in Page_Init, how can I respond to an
    >> IndexChanged event, which doesn't happen until *after* Page_Load?
    >> I know I should rebuild the controls every time. One of my problems is that
    >> I the information I need to decide *what* to (re)build isn't available yet
    >> at the time I should do it.
    >>
    >> And an additional question: does Ajax change that lifecycle? (the pulldown
    >> and the properties section ar in an UpdatePanel).
    >>
    >> Hans Kesting
    >>
    >>

    >
    > I see.. for the event part you can (after creating it dynamically):
    >
    > MyDynamicControl.OnIndexChanged += new [Handler]
    >
    > Mike


    I know. The event does fire, my handler gets executed and adds the
    'property' fields to the page, so the page looks OK. However when I
    then hit the "save" button (after selecting values in the dropdowns of
    some properties) those values are lost.
    When I revisit the item, the dropdown is filled before the Load and the
    property-section is created in the Load event. Now the values in the
    property-pulldowns are saved correctly.

    So how can I get the property-section to behave correctly after an
    "index changed"?


    Hans Kesting
    Hans Kesting, Nov 3, 2008
    #5
  6. Maybe Instead of adding dynamic controls in the event handler (selected
    index changed), you can create your dynamic controls in the page Init
    function when the selected index is discovered:

    You can check if the selected index changed by looking at those in you page
    Init:

    HttpContext.Current.Request.Form["__EVENTTARGET"]
    HttpContext.Current.Request.Form["__EVENTARGUMENT"]

    Mike
    Mike Gleason jr Couturier, Nov 3, 2008
    #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. John Lau

    Page and control lifecycle

    John Lau, May 11, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    513
    John Lau
    May 11, 2004
  2. redhair
    Replies:
    1
    Views:
    422
    Scott Allen
    Nov 30, 2004
  3. Frankie
    Replies:
    2
    Views:
    5,967
    Frankie
    Jun 23, 2005
  4. Jonny

    User Controls Event Lifecycle

    Jonny, Sep 13, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    7,877
    Scott Allen
    Sep 14, 2005
  5. Sky
    Replies:
    7
    Views:
    641
Loading...

Share This Page