ASP.NET生命周期二分法:动态添加的控件和事件 [英] ASP.NET Life-cycle Dichotomy: Dynamically Added Controls and Events

查看:140
本文介绍了ASP.NET生命周期二分法:动态添加的控件和事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:



  • 我拥有带有挂钩事件的按钮的用户控件。需要在Page_Load或更早版本中初始化具有事件的控件。

  • 我希望通过单击添加按钮动态生成这些用户控件。

  • 重要的是要记住事件(例如点击事件)在Page_LoadComplete之前不会触发。



  • 破解解决方案A:


    1. Page_Load:动态创建一个添加按钮,挂钩点击事件。

    2. 用户单击添加按钮,触发回发。

    3. Page_Load:像往常一样创建添加按钮,并挂钩点击事件。

    4. Page_Load:不知道添加按钮已经被点击了,所以它不知道产生用户控件!

    5. AddButton_Click:页面最后知道应该在下一个Page_Load上添加新的用户控件。

    6. 由于另一个Page_Load已被触发,用户看不到他们添加的控件。

    7. 用户通过单击按钮,刷新页面等来重新加载页面。

    8. Page_Load:创建添加按钮,并点击事件。现在意识到添加的用户控件,创建用户控制。在用户控制下挂钩事件。

    9. 用户点击用户控制中的按钮,触发器很好。



    结果:用户点击添加新的用户控件,服务器知道该控件应该存在,但用户直到触发页面再次加载(通过单击另一个按钮或刷新等)才看到它)



    自然而然,我看了一下生命周期,看到Page_LoadComplete发生在事件之后,所以如果我将任何与事件相关的代码放在Page_LoadComplete中,那么应该好吗



    破解解决方案B:


    1. Page_LoadComplete:动态创建一个添加按钮,点击事件

    2. 用户点击添加按钮,触发回发。

    3. Page_LoadComplete:像往常一样创建添加按钮,并挂钩点击事件。 / li>
    4. AddButton_Click:页面意识到在下一个Page_LoadComplete中应该添加一个新的用户控件。

    5. Page_LoadComplete:意识到按钮的点击,动态添加控件,使用自己的内部按钮点击事件。

    6. 用户点击添加的用户控件中的按钮,但它不触发!!

    结果:一切都很好,除了添加的用户控件中的按钮是惰性的。 p>

    难题是:我需要通过按钮点击产生的控件,这意味着我需要把我的 Controls.Add(...) code> Page_LoadComplete中的代码。相反,我需要添加的控件具有工作事件,这意味着 Controls.Add(...)代码需要在Page_Load中。我有一个完美的二分法。



    我可以想到的唯一的janky解决方案是采取解决方案A,并强制页面重新加载手动点击添加按钮,这是一个浪费的带宽和处理。

    解决方案

    这可能不是最佳做法,但可以查看请求。表单用于在Page_Init事件期间添加按钮单击的证据,并在其中添加控件。它应该可以解决你的问题。


    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.

    解决方案

    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.

    这篇关于ASP.NET生命周期二分法:动态添加的控件和事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆