We recommend that you use override instead of event handler on the ASP. NET page)

Source: Internet
Author: User

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

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.