简体   繁体   English

Loadcontrol事件正在运行,但是如何?

[英]Loadcontrol event Is working but how?

Page Code : 页面代码:

public partial class Default2 : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        Uctest ctrl = (Uctest) this.LoadControl("Uctest.ascx");
        ctrl.ID = "aaa111";
        Page.Controls.Add(ctrl);
    }
}

Ascx Code : ASCII代码:

public partial class Uctest : UserControl
{
    protected void btn1_Click(object sender, EventArgs e)
    {
    }
}

I have a page and ascx . 我有一个页面ascx the ascx contains asp:button and asp:textbox ascx包含asp:buttonasp:textbox

My goal is to reach to the 'btn1_Click' event. 我的目标是达到“ btn1_Click”事件。

this is working. 这正在工作。 - when i press the button it goes to the event and its all ok. -当我按下按钮时,它将转到该事件,一切正常。

but i cant understand why. 但我不明白为什么。 because each postback it is RECREATING the CONTROL AGAIN , so how does he knows to attch the pressed event to the upcoming new created ascx (because of the postback) ?? 因为每次回发时则再次重现控制,所以他怎么知道要按下的事件attch 即将到来的新创建的ascx(因为回传的)?

When you post back, you're re-creating the user control. 回发时,您正在重新创建用户控件。

It is the user control that contains the event method and it is the user control that sets up the event delegation, so it makes sense that each time you create an instance of the user control, it also creates the event method and attaches to it. 包含事件方法的是用户控件,而设置事件委托的是用户控件,因此有意义的是,每次创建用户控件的实例时,它还会创建并附加到事件方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Load事件中使用LoadControl的陷阱 - Pitfalls of using LoadControl in the Load event LoadControl和Page_Load事件未触发 - LoadControl and Page_Load event not firing 如何在LoadControl调用中引用类类型? - how to reference class type in a LoadControl call? 静态脚本方法中的LoadControl不遵守页面生命周期(不引发Init事件) - LoadControl in a static scriptmethod does not adhere page lifecycle (Init event is not raised) 使用loadcontrol时未触发ASP.NET onselectedindexchanged事件 - ASP.NET onselectedindexchanged event not firing when using loadcontrol 如果通过 LoadControl(ctr) 加载 UserControl.ascx,则事件不起作用 - Events are not working if UserControl.ascx are loaded through LoadControl(ctr) 如何加载控制使用 VaryByControl OutputCache 的控件,指定属性值 - How to LoadControl a control that uses VaryByControl OutputCache, specifying values for properties 如何使用LoadControl方法动态加载UserControl(Type,object []) - How to load a UserControl dynamically with LoadControl Method (Type, object[]) 我如何通过form.Controls.Add(page.LoadControl)传递一些文本 - How can I pass some textes with form.Controls.Add(page.LoadControl) ASP.NET:如何使用LoadControl(字符串路径)调试动态加载的用户控件; - ASP.NET: How do you debug dynamically loaded user controls using LoadControl(string path);
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM