Http://dflying.dflying.net/1/archive/101_prefer_overrides_to_event_handlers_in_aspnet_page.html
This is something we are familiar.Page_load ()Method. It is actuallyEvent HandlerWhen defined inSystem. Web. UI. PageInLoadWhen an event is triggered, it starts to run.
// Use event handler
Protected Void Page_load ( Object Sender, eventargs E)
{
//Logic here
}
This isSystem. Web. UI. PageClassOnload ()MethodOverride.
// 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 that you useOverride.
First, the event processing mechanism is used to implement communication between independent objects. For exampleButtonWhen clicked, the page can learn and process the message through the event processing mechanism. But in this example,LoadThe event is defined inSystem. Web. UI. PageIs part of our page. In this way, it will be strange to issue an event within a class and handle it in itself.
Second, in terms of efficiency, the event processing capability is insufficient.Override. This is caused. NET FrameworkAnd we all know about the implementation.
In addition, when using events, you need to maintain two places: loading the event processing method (Attach) And the definition of the event processing function itself. AlthoughASP. Network 2.0Some predefined event handling method names have been provided, but many developers are still usingASP. Network 1.1. And useOverrideYou only need to maintainOverrideFunction itself.
Of course, the event processing mechanism has its own advantages. For example, you can easily specify the event processing method at runtime, and allow multiple event processing methods distributed in various places to be executed in sequence. HoweverASP. NETWe will not use these features on the page. There will always be a way to define the page loading behavior, and we will not have multiplePage_load ()Method appears on a page.
More generalized, not just inASP. NETIn other cases, we should try to useOverrideInsteadEvent.
UseOverrideDo not forget to call the method of the base class (Visual StudioWill do it for you ).