When ASP. NET is used, the first line of the page command of the newly created ASPX page contains an autoeventwireup attribute. Many tutorials on the Internet think that this attribute and even this line of code are useless. In fact, this is not familiar with ASP. NET event processing model. Simply put, this attribute determines whether the current page is automatically associated with certain special events.
First, events triggered from the browser page cannot be processed locally immediately, but are post to the server. Therefore, ASP. NET sets up a delegate (proxy) mechanism. At the same time of setting up an event, establish the corresponding delegate:
Private void initializecomponent ()
{
This. mybutton. Click + = new system. eventhandler (this. button#click); // delegate
}
Private void button#click (Object sender, system. eventargs E)
{
// Event content
}
The delegate explicitly associates the event with the page. When autoeventwireup = "false", the event is not executed without delegation.
This is different from the implicit event suspension adopted by early VB, and has its own advantages and disadvantages. However, in ASP. NET, you can modify autoeventwireup = "true" to bind the page to some special event methods and automatically identify these events with specific names without delegation. Specific names include page_init, page_load, page_databind, page_prerender, and page_unload. For example:
Private void page_load (Object sender, system. eventargs E)
{
// Event content
}
For these event methods, developers can use this parameter to avoid writing too much link code. If this attribute is set to "false", the code should be changed:
Private void initializecomponent ()
{
This. mybutton. Click + = new system. eventhandler (this. button#click); // delegate
This. Load + = new system. eventhandler (this. page_load); // you can use the autoeventwireup attribute to avoid delegation.
}
Private void page_load (Object sender, system. eventargs E)
{
// Event content
}
Private void button#click (Object sender, system. eventargs E)
{
// Event content
}
The page_load method cannot be used without delegation!
In most cases, the page does not need to be associated with so many special events, and additional operations and system overhead will be added. At the same time, the ASP. NET page framework automatically calls the event processing method based on its predefined name, which leads to the same event processing method. When the page is called twice, the system overhead is also increased. Therefore, Microsoft recommends that you always set autoeventwireup to false.
@ Page instructions:
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