Dynamically Loaded User Control Event Wiring

Discussion in 'ASP .Net' started by Andrew Robinson, May 23, 2007.

  1. I have a page that can load a number of different user controls. Each of
    these user controls inherits from a common base class and the controls are
    loaded based on application state, status, etc and the specific type of
    control frequently changes. I have a common event in the base class that
    each child user control inherits from and that the page wires up and uses to
    get status from the child user control. I hope all pretty simple and not too
    far out there....

    I am loading and wiring the control in the Page_Load event or during a
    secondary event in reponse to a button click on the page (in the page
    class). The user control is added to a place holder control on the page.

    This all works fine but somehow I am not receiving the initial invocation on
    of the event on the child user control. It does work the second time around.

    Bottom line: during which event on a page should I be loading and wiring
    events on a dynamically loaded user control?

    Any help on this would be appriciated.

    -Andy
     
    Andrew Robinson, May 23, 2007
    #1
    1. Advertising

  2. "Andrew Robinson" <> wrote in message
    news:...
    >I have a page that can load a number of different user controls. Each of
    >these user controls inherits from a common base class and the controls are
    >loaded based on application state, status, etc and the specific type of
    >control frequently changes. I have a common event in the base class that
    >each child user control inherits from and that the page wires up and uses
    >to get status from the child user control. I hope all pretty simple and not
    >too far out there....
    >
    > I am loading and wiring the control in the Page_Load event or during a
    > secondary event in reponse to a button click on the page (in the page
    > class). The user control is added to a place holder control on the page.
    >
    > This all works fine but somehow I am not receiving the initial invocation
    > on of the event on the child user control. It does work the second time
    > around.
    >
    > Bottom line: during which event on a page should I be loading and wiring
    > events on a dynamically loaded user control?
    >


    The best place to load controls is in the Page_Init() method, I think.
     
    Alexey Smirnov, May 23, 2007
    #2
    1. Advertising

  3. Andrew Robinson

    bruce barker Guest

    if you dynamically add a control, on the next postback and all
    subsequent postbacks) you should re-add the control and wireup events in
    the oninit event.

    -- bruce (sqlwork.com)



    Andrew Robinson wrote:
    > I have a page that can load a number of different user controls. Each of
    > these user controls inherits from a common base class and the controls
    > are loaded based on application state, status, etc and the specific type
    > of control frequently changes. I have a common event in the base class
    > that each child user control inherits from and that the page wires up
    > and uses to get status from the child user control. I hope all pretty
    > simple and not too far out there....
    >
    > I am loading and wiring the control in the Page_Load event or during a
    > secondary event in reponse to a button click on the page (in the page
    > class). The user control is added to a place holder control on the page.
    >
    > This all works fine but somehow I am not receiving the initial
    > invocation on of the event on the child user control. It does work the
    > second time around.
    >
    > Bottom line: during which event on a page should I be loading and wiring
    > events on a dynamically loaded user control?
    >
    > Any help on this would be appriciated.
    >
    > -Andy
     
    bruce barker, May 23, 2007
    #3
  4. Ok, one more piece of missing info: The particular flavor of my dynamic user
    control is determined by info that is stored in ViewState on the host page
    (which is not loaded until Page_Init). Seems like a chicken and egg issue.

    Thanks,


    "bruce barker" <> wrote in message
    news:...
    > if you dynamically add a control, on the next postback and all subsequent
    > postbacks) you should re-add the control and wireup events in the oninit
    > event.
    >
    > -- bruce (sqlwork.com)
    >
    >
    >
    > Andrew Robinson wrote:
    >> I have a page that can load a number of different user controls. Each of
    >> these user controls inherits from a common base class and the controls
    >> are loaded based on application state, status, etc and the specific type
    >> of control frequently changes. I have a common event in the base class
    >> that each child user control inherits from and that the page wires up and
    >> uses to get status from the child user control. I hope all pretty simple
    >> and not too far out there....
    >>
    >> I am loading and wiring the control in the Page_Load event or during a
    >> secondary event in reponse to a button click on the page (in the page
    >> class). The user control is added to a place holder control on the page.
    >>
    >> This all works fine but somehow I am not receiving the initial invocation
    >> on of the event on the child user control. It does work the second time
    >> around.
    >>
    >> Bottom line: during which event on a page should I be loading and wiring
    >> events on a dynamically loaded user control?
    >>
    >> Any help on this would be appriciated.
    >>
    >> -Andy
     
    Andrew Robinson, May 23, 2007
    #4
  5. Hi Andrew,

    As for dynamic created control, they need to be added into Page's control
    hierarchy(and wireup event handler) in either Page's Init or Load event.
    Also, for ViewState, it is loading after Init and Before Load event. Thus,
    if you have some dynamic control generation code logic that depend on
    variables in ViewState, you need to use Load event instead of Init event.

    One way to avoid using viewstate and also use Init for conditional dynamic
    control generation is use a hidden field on page to store the certain
    variable that control the dynamic control generation. Such hidden field can
    be access through Request.Form collection directly in Init event(before
    viewstate load).

    Anyway, if possible, you can try show us a simplified page that can repro
    the problem you met so that we can have a further look into it.


    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], May 24, 2007
    #5
  6. Hi Andrew,

    Have you got any further idea on this issue or does the suggestion in
    previous reply help you a little?
    Please feel free to post here if there is anything else we can help.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], May 28, 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. Thanh Nguyen
    Replies:
    0
    Views:
    447
    Thanh Nguyen
    Apr 1, 2004
  2. Tonix
    Replies:
    0
    Views:
    424
    Tonix
    Aug 24, 2004
  3. Jordan
    Replies:
    4
    Views:
    627
    Bruce Barker
    Sep 1, 2005
  4. Sosh
    Replies:
    5
    Views:
    4,131
    =?Utf-8?B?V296emE=?=
    Nov 16, 2005
  5. bill
    Replies:
    1
    Views:
    407
    Ken Cox - Microsoft MVP
    Apr 9, 2006
Loading...

Share This Page