AutoEventWireup True Vs False

I use the professional edition of Visual Studio 2012. I do not see any difference in setting "true" compared to "false" for the AutoEventWireup property in the page directive. All the time it behaves like “truth”, “Meaning” - I set the “false” and non-binding events explicitly, but the events implicitly get a binding. Please let me know if I don’t miss anything.

+4
source share
2 answers

This parameter is not intended to trigger an event, but to bind handlers to standard page events. Compare these two snippets that illustrate the handling of the Load event.

First with AutoEventWireup="true" :

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

Secondly, with 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"); } } 

The Load event will be triggered by the page and processed by your code in both cases. But in the second case, you must subscribe to the event explicitly, while in the first case, ASP.NET will do everything for you.

Of course, the same goes for other page life cycle events such as Init , PreRender , etc.

+8
source

I know this is an old thread, but decided that I would add the following, which recently helped me:

In addition to Andrei’s answer, it’s worth adding that by setting AutoEventWireup to “true”, you run the risk of calling Page_Load () twice each time the page loads. It happened to me. He fully explained here from where I copied the following:

Do not set AutoEventWireup to true if performance is a key consideration. When automatic event posting is enabled, ASP.NET must between 15 and 30 attempts to map events to methods.

Note the following about binding event handlers to events:

  • If AutoEventWireup is set to true, make sure that you also do not manually attach page event handlers to events. If you do this, handlers can be called more than once.

  • Auto-binding is performed only for page events, not events for controls on the page.

  • As an alternative to attaching events to handlers, you can override the Oneventname methods of the page or controls.

+3
source

All Articles