Summarize Page_Load and OnLoad

Source: Internet
Author: User
Original source

First Page_Load is a method, and onload is a method.

OnLoad is the page class inherits from System.Web.UI.TemplateControl, and System.Web.UI.TemplateControl inherits from System.Web.UI.Control. So strictly speaking, the onload is a method that belongs to System.Web.UI.Control.

Also page has a load event that is also the same inherited from System.Web.UI.Control.

Here you will introduce a autoeventwireup in the @ Page directive. When Autoeventwireup=true, the page is bound to some special event methods to automatically recognize these events with a specific name. These specific names include: Page_Init, Page_Load, Page_databind, Page_PreRender and Page_Unload.

When True,asp.net does not need to be displayed to bind event handlers, it automatically binds events based on their method name and signature at run time, and ASP.net will search for each event according to the page_eventname pattern. If an event handler with this signature is not found, ASP.net checks for overloads that do not have parameters. When False, you must show the binding event method, for example, you have a Page_Load method, only when you write the code, will trigger the event method: Page.load + = Page_Load; In this case, the method name does not have to follow a pattern. When considering performance, do not set AutoEventWireup to true. When an automatic event connection is enabled, ASP.net must make 15 to 30 attempts to match the event with the method.

The page's Load event, exactly, is the System.Web.UI.Control load event bound by the System.Web.UI.Control onload method. So if you rewrite the OnLoad method in the page, this is autoeventwireup=true, but the Page_Load method is not executed.

Therefore, if performance is considered, it is recommended that the override OnEventName method be used to implement the event loading of the page.

There are several methods in the System.Web.UI.Control class that can be used for override

protected internal virtual void OnInit (EventArgs e);

protected internal virtual void OnLoad (EventArgs e);

protected internal virtual void OnPreRender (EventArgs e);

protected internal virtual void OnUnload (EventArgs e);

Can be overridden as follows

protected override void OnLoad (EventArgs e)

{

Response.Write ("onload<br>");

}

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.