First, let's take a look at the twoCode
The first method is the familiar page_load () method. In fact, this is an eventhandler. When the load event defined in system. Web. UI. Page is triggered, it starts to execute
// Use Event Handler
Protected void page_load (Object sender, eventargs E)
{
//
}
This is the override of the onload () method in the system. Web. UI. Page class. // Use override
Protected override void onload (eventargs E)
{
Base. onload (E );
}
Although both of the above methods can accomplish the same function, I recommend using override.
First, the event processing mechanism is used to implement communication between independent objects. For example, when a button is clicked, the page can learn and process the message through the event processing mechanism. In this example, the load event is defined in system. Web. UI. page, which is 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, event processing is inferior to override. This is determined by the implementation of the. NET Framework and we all know about it.
In addition, the event processing method attach and the definition of the event processing function must be maintained. Although ASP. NET 2.0 already provides some predefined event processing method names, many developers are still using ASP. NET 1.1. To use override, you only need to maintain the override function 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. However, these features are not used in ASP. NET pages. We will always have a method to define the page loading behavior, and we will not have multiple page_load () methods in one page.
A little more general, not only in ASP. NET pages, we should also try to use override rather than event in other cases.
When using override, note that you should not forget to call the methods of the base class (Visual Studio will do it for you ).