开发者

ASP.NET Life-cycle Dichotomy: Dynamically Added Controls and Events

开发者 https://www.devze.com 2023-01-14 06:35 出处:网络
The situation: I have user controls with buttons that have hooked events.Controls with events need to be initialized in Page_Load or earlier.

The situation:

  • I have user controls with buttons that have hooked events. Controls with events need to be initialized in Page_Load or earlier.
  • I wish to spawn these user controls dynamically by clicking an Add button.
  • It is important to remember that events, such as click events, are not fired until just before Page_LoadComplete.
  • Broken Solution A:

    1. Page_Load: Dynamically create an Add button, hook click event.
    2. User clicks on the Add button, triggers postback.
    3. Page_Load: Creates the Add button as usual, and hooks click event.
    4. Page_Load: Doesn't know that the Add button has been clicked yet, so it doesn't know to spawn the user control!
    5. AddButton_Click: Page fina开发者_高级运维lly aware that a new user control should be added upon the next Page_Load.
    6. User cannot see the control they added, because another Page_Load has been triggered.
    7. User reloads the page by clicking a button, refreshing the page, etc.
    8. Page_Load: Creates Add button, and hooks click event. Now aware of added user control, creates user control. Hooks events within the user control.
    9. User clicks on button within user control, triggers just fine.

    Result: User has clicked to Add a new user control, the server is aware that the control should exist, but the user does not see it until they trigger the page to load again (by clicking another button, or refreshing, etc).

    Naturally, I took a look at the life-cycle, and see that Page_LoadComplete occurs after events, so if I place any event-dependent code in Page_LoadComplete, all should be well?

    Broken Solution B:

    1. Page_LoadComplete: Dynamically create an Add button, hook click event.
    2. User clicks on the Add button, triggers postback.
    3. Page_LoadComplete: Creates the Add button as usual, and hooks click event.
    4. AddButton_Click: Page aware that a new user control should be added upon the next Page_LoadComplete.
    5. Page_LoadComplete: Aware of the button click, dynamically adds the control, with its own internal button click event.
    6. User clicks on button within the added user control, but it does not trigger!!

    Result: Everything works great, except the button within the added user control is inert.

    The conundrum is: I need controls to spawned by a button click, which means I need to put my Controls.Add(...) code in Page_LoadComplete. Inversely, I need the controls being added to have working events, which means the Controls.Add(...) code need to be in Page_Load. I have a perfect dichotomy.

    The only janky solution I can think of is taking solution A and forcing the page to reload manually after clicking the Add button, which is a waste of bandwidth and processing.


    This might not be the best practice, but you can check Request.Form for the evidence of add button click during the Page_Init event and add the controls there. It should solve your problem.

    0

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    关注公众号