English Version: http://dflying.dflying.net/1/archive/101_prefer_overrides_to_event_handlers_in_aspnet_page.html
This is the Page_Load() method we are familiar with. In fact, it is an Event Handler, which starts executing when the Load event defined in System.Web.UI.Page is triggered.
// use event handler
protected void Page_Load(object sender, EventArgs e)
{
// logic here
}
This is the Override of the OnLoad() method in the System.Web.UI.Page class. // use override
protected override void OnLoad(EventArgs e)
{
// logic here
base.OnLoad(e);
}
Although both of the above methods can accomplish the same function, I recommend using the Override method.
First of all, the event handling mechanism is used to implement communication between independent objects. For example, when a Button is clicked, the page can learn this message through the event handling mechanism and process it. But in this example, the Load event is defined in System.Web.UI.Page and is itself part of our page. It would be strange to emit an event from within a class and then handle the event itself.
Secondly, in terms of efficiency, event handling is not as good as Override. This is determined by the implementation of the .NET Framework and we all know it.
Also, two places need to be maintained when using events: the attachment of the event handling method and the definition of the event handling function itself. Although ASP.NET 2.0 already provides some predefined event handling method names, there are still many developers using ASP.NET 1.1. When using Override, you only need to maintain the Override function itself.
Of course, the event processing mechanism has its own benefits. For example, it is easy to specify event processing methods at runtime, allowing multiple event processing methods distributed everywhere to be executed in sequence. But we will not use these features in ASP.NET pages. We will always have a method to define the behavior of the page being loaded, and we will not have multiple Page_Load() methods appearing on a page.
To be more general, not only in ASP.NET pages, we should also use Override instead of Event in other situations as much as possible.
When using Override, be careful not to forget to call the base class method (Visual Studio will do it for you).
Source: Dflying Chen BLOG