[英]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:button和asp: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.