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

不羁的心 提交于 2019-12-13 02:53:05

问题


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 finally 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.


    回答1:


    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.



    来源:https://stackoverflow.com/questions/3622479/asp-net-life-cycle-dichotomy-dynamically-added-controls-and-events

    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!