AutoEventWireup
on the Page
element works by looking for methods matching an expected signature and naming convention (i.e. Page_<event>
). If found, the page framework will call the event-handlers directly before "raising" the event normally. This is why, if you register the event-handlers in code, they are effectively called twice.
This is quite different from how event handlers normally work. If you wrote code to register a handler with the same event twice, it will only be called once per event. This is because the event handlers are essentially a set of delegates and attempting to add a delegate to the same method is a no-op.
The AutoEventWireup
behaviour only applies to events raised by the page. You won't get it for other controls, so they should be wired up manually, usually in a Page_Init
or Page_Load
event.
If you don't use AutoEventWireup
(i.e. set it to "false"), you have to wire up the page event handlers yourself to get them to be called. This is what the Visual Studio designer does, since it generates event-registration code for you.