Asp. Net (exact meaning of the AutoEventWireup attribute)
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.
Sometimes you will find that page loading takes a long time. During debugging, it is found that Page_Load is executed twice.
The reason is that you removed AutoEventWireup from the ASPX page.,Or set its property to True:
AutoEventWireup = "false.
<% @ Page language = "c #" Codebehind = "ShowSimpleFactory. aspx. cs" AutoEventWireup = "false" Inherits = "test. DesignPatternTest. ShowSimpleFactory" %>
So what is AutoEventWireup?
If the AutoEventWireup attribute of the Page command is set to true (or if this attribute is missing because it is set to true by default), the Page framework automatically calls the Page events, namely the Page_Init and Page_Load methods. In this case, no explicit Handles clause or delegation is required.
In Web server controls, some events (usually Click events) may cause the form to be sent back to the server. Changes in HTML server controls and Web server controls (such as TextBox controls) will be captured, but will not be sent immediately. Instead, they will be cached by the control until the message is sent again. Then, when the page is processed on the server again, all pending events are raised and processed.
Web server controls that support event changes include the AutoPostBack attribute. When this attribute is set to true, the control's change event will cause the form to be sent immediately without waiting for the Click event. For example, by default, the CheckedChange event of the CheckBox control does not cause the page to be submitted. However, by setting the AutoPostBack attribute of the control to true, you can specify that when you click the check box, the page is immediately sent to the server for processing.
PS: there is also a saying that Page_Load is also executed twice on the Default page!