Rewriting onload is better than page_load ().

Source: Internet
Author: User

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 ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.