AutoEventWireup True Vs False

afi*_*fin 4 asp.net visual-studio-2012

我正在使用Visual Studio 2012专业版.我认为在page指令中为AutoEventWireup属性设置"true"与"false"没有任何区别.它一直表现为"true",意思是 - 我设置"false"而不是显式绑定事件,但事件是隐式绑定的.如果我遗失任何东西,请告诉我.

And*_*rei 8

此设置不是关于触发事件,而是将处理程序绑定到标准页面事件.比较这两个说明处理Load事件的片段.

首先,用AutoEventWireup="true":

public class PageWithAutoEventWireup
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("Page_Load is called");
    }
}
Run Code Online (Sandbox Code Playgroud)

第二,有AutoEventWireup="false":

public class PageWithoutAutoEventWireup
{
    override void OnInit(EventArgs e)
    {
        this.Load += Page_Load;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("Page_Load is called");
    }
}
Run Code Online (Sandbox Code Playgroud)

Load在两种情况下,事件都将按页面触发并由您的代码处理.但在第二种情况下,您必须明确注册该事件,而在第一种情况下,ASP.NET会为您完成所有事情.

当然,这同样适用于其他页面生命周期事件,如Init,PreRender等.