Reputation: 3200
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.
Upvotes: 2
Views: 2412
Reputation: 8241
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
Upvotes: 1
Reputation: 58969
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.
Upvotes: 3
Reputation: 77697
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.
Upvotes: 0