简体   繁体   中英

What value should AutoEventWireup be set to in the page directive in ASP.Net?

I am using C#. By default, when I add a web form in Visual Studio 2008 with or without a master page, the AutoEventWireup attribute is set to true in the page directive. This attribute is also set to true inside the master page master directive.

What value should I have AutoEventWireup set to (true/false)?

What are the pros and cons of both values?

Any help is greatly appreciated.

Thank you.

That is a way of automatically wiring up event handlers to events based on naming conventions that Microsoft has setup.

The way this is implemented is with reflection, if I remember correctly. At runtime, ASP.NET will inspect your class, look for methods with signatures that match the naming convention expected, and then wire those up as handlers for their respective events.

That said, the pros are that it is a standard approach and saves you the trouble of wiring up the event handlers yourself. A perceived "con" would be that it takes an extra step (reflection) that costs a bit more than if you were to do it yourself.

For the most part, the reflection "cost" is so little that it really isn't worth mentioning, but it is important to be aware of what is happening under the covers.

That just causes runtime to automatically hook up the conventional page lifecycle methods like Page_Load to their equivalent eventhandler (Page.Load). If you set AutoEventWireup to false, then you need something like:

protected override void OnLoad(EventArgs e)
{
   base.OnLoad(e); 
}

instead of the MS convention of:

protected void Page_Load(object sender, EventArgs e)
{
}

You also don't have to worry about calling base.OnLoad, because the wireup automatically does that. But, there might be a small performance benefit from setting it to false - I have never verified that though.

-Nate Davis

If it's set to false, then if you have methods like Page_Load or Page_PreInit, they wouldn't ever fire. AutoEventWireup means that IF there are events named like that, they should have the event handler wired up to those events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM