The exact meaning of the autoeventwireup attribute

Source: Internet
Author: User
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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.