The exact meaning of the AutoEventWireup attribute

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

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.