tags:

views:

471

answers:

3

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.

A: 

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.

Darren Kopp
+3  A: 

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.

Jason Bunting
+1  A: 

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

NathanD
This is mostly incorrect - the first method you show is not an event handler, but an override. There is a huge difference and I suggest you read up on the subject. The way you would have to wire-up things manually is to have code, typically in the constructor, like this:
Jason Bunting
this.Load += new EventHandler(MyPage_Load);
Jason Bunting
Then, you would implement MyPage_Load with the same signature that you would see for the automatically-generated Page_Load handler:
Jason Bunting
protected void MyPage_Load(object sender, EventArgs e) { }
Jason Bunting
Wrong jason, he is correct. i think you should brush up on the subject. The virtual method is what calls the event, but are called at the EXACT SAME TIME. you should read before you down vote.
Darren Kopp
Jason, I never said that my first example code was an event handler - only that it was a way to hook into the Load event if you set AutoEventWireup to false. Also, why would I create a MyPage_Load event handler method rather than just override OnLoad? That is unconventional and doesn't make sense.
NathanD