When you use ASP.net, the first line of the new ASPX page contains a AutoEventWireup attribute in the page directive. Many tutorials on the Web think that this property, even this line of code is useless. In fact, this is not understanding the performance of the ASP.net event handling model. In simple terms, this property determines whether the current page automatically associates certain special events.
First, events triggered from a browser page are not immediately processed locally, but are posted to the server, so ASP.net establishes a delegate (proxy) mechanism. Establish an appropriate delegate while creating an event:
private void InitializeComponent ()
{
This.mybutton.Click + = new System.EventHandler (this. button1_click);/delegate
}
private void Button1_Click (object sender, System.EventArgs e)
{
Event contents
}
The delegate explicitly associates an event with the page. When autoeventwireup= "false", the event will not execute if there is no delegate.
This is different from the implicit event hanging in the early VB, each has its own advantages and disadvantages. However, ASP.net can modify autoeventwireup= "true" so that the page is bound to certain special event methods to automatically recognize these events with a specific name, without requiring a delegate. These specific names include: Page_Init, Page_Load, Page_databind, Page_PreRender and Page_Unload. Like what:
private void Page_Load (object sender, System.EventArgs e)
{
Event contents
}
For these event methods, developers can use this parameter to avoid writing too many linked code. If this property is set to False, the code should read:
private void InitializeComponent ()
{
This.mybutton.Click + = new System.EventHandler (this. button1_click);/delegate
This. Load + = new System.EventHandler (this. Page_Load)//delegate that can be avoided with the AutoEventWireup property
}
private void Page_Load (object sender, System.EventArgs e)
{
Event contents
}
private void Button1_Click (object sender, System.EventArgs e)
{
Event contents
}
You must not use a Page_Load similar method without a delegate.
In most cases, the page does not need to be associated with so many special events, adding additional operational and overhead. At the same time, the ASP.net page framework automatically invokes event handling based on its predetermined name, which causes the system overhead to be increased by the same event-handling method when the page is run two times. Therefore, Microsoft recommends that you always set AutoEventWireup to FALSE.
Description of @ Page directive:
HTTP://MSDN2.MICROSOFT.COM/EN-US/LIBRARY/YDY4X04A (vs.71). aspx
ASP.net server control event model
HTTP://MSDN2.MICROSOFT.COM/EN-US/LIBRARY/59T350K3 (vs.71). aspx