30

Page_Load isn't a virtual method. What calls this method and how does it do it? Is it reflection or some other technique? Also how many events are handled this way?

Also is it preferable to handle things in an overloaded OnLoad or Page_Load? How are they different?

Orion Adrian
  • 19,053
  • 13
  • 51
  • 67

5 Answers5

27

ASP.NET has a feature called "AutoEventWireup" - this feature allows you to create methods that have the EventHandler signature with names like Page_Load and the runtime will wire up the event from the parent page to the method in your class. Basically the runtime does this on your behalf:

this.Load += this.Page_Load;

Now it is better to disable AutoEventWireup and either create these event handlers yourself in the pages OnInit method or simply override the parent page's OnLoad method.

Edit (in response to the OP's comment below): This process doesn't cover button clicks and such but the process is similar.

In order for a method like MyButton_Click to work without you explicitly creating an event handler you would have to set the OnClick attribute on the control in the aspx file like this:

<asp:button 
    id="MyButton"
    onClick="MyButton_Click"
    runat="server" />

This would prompt ASP.NET to create the button click delegate for you and attach it to the button's Click event.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • does this cover controls with IDs as well? Could you write MyButton_Click? – Orion Adrian Sep 29 '09 at 20:00
  • 2
    Why the recommendation to disable AutoEventWireup? – M4N Sep 29 '09 at 20:01
  • @Martin: The delegate generation for the wireup happens at execution time and is slower than just overriding a method. @Orion: It doesn't cover button clicks and such but the process is similar. In order for a method like `MyButton_Click` to work without you explicitly creating an event handler you would have to set the `OnClick` attribute on the control in aspx file like this: `OnClick="MyButton_Click"`. This would prompt ASP.NET to create the button click delegate for you and add it to the button's `Click` event. – Andrew Hare Sep 29 '09 at 20:06
  • So, technically, to answer the original question; the base class uses reflection to add your Thing_Event methods as event handlers for events that are raised. Right? – bzlm Jan 06 '10 at 14:20
5

The order in which the virtual methods (OnLoad) and event handlers (Page_Load) are called is defined by the so called page lifecycle. This is just the way how the ASP.NET runtime processes an incoming request (e.g. with the Init, Load, Render stages).

You can use either OnLoad or Page_Load but you have to be aware of what happens:

  • inside OnLoad you must call base.OnLoad
  • inside base.OnLoad the Load event will be raised
  • Page_Load is a handler for the Load event (which is automatically wired-up) and will therefore be invoked as a result of the Load event that's being raised.

If you do not call base.OnLoad in your OnLoad override, then the Load event will not be raised.


Update: you can use an empty page with the following code-behind to see what happens:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    base.Load += new EventHandler(My_Page_Load);
}

void My_Page_Load(object sender, EventArgs e)
{
    Response.Write("My_Page_Load<br/>");
}

protected override void OnLoad(EventArgs e)
{
    Response.Write("Start of OnLoad<br/>");
    base.OnLoad(e);
    Response.Write("End of OnLoad<br/>");
}

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("Page_Load<br/>");
}

Try commenting the base.OnLoad(e) call and see again.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
M4N
  • 94,805
  • 45
  • 217
  • 260
2

The OnLoad method somewhere up the Page hierarchy calls the events assigned to Load (via +=).

The naming Page_Load is just a convention. In AutoEventWireUp mode (i.e. no event handler explicitly declared) this convention is used to find event handlers by their names.

If you have .Net1 available, you can see how the designer adds code to the page's OnInit() to add all components of the page and set

this.Load += new System.EventHandler(this.Page_Load);

.Net2 still does that, but in a separate file which is hidden somewhere under the Windows\Microsoft.Net\Framework\v*\Temporary ASP.Net Files.

I find this chart on ASP.Net Page Life Cycle very useful.

devio
  • 36,858
  • 7
  • 80
  • 143
1

Take a look at the ASP.NET page lifecycle, there is a section for lifecycle events where it describes load.

Load
The Page calls the OnLoad event method on the Page, then recursively does the same for each child control, which does the same for each of its child controls until the page and all controls are loaded. Use the OnLoad event method to set properties in controls and establish database connections.

Further Quote:

Note that when creating an event handler using the Page_event syntax, the base implementation is implicitly called and therefore you do not need to call it in your method. For example, the base page class's OnLoad method is always called, whether you create a Page_Load method or not. However, if you override the page OnLoad method with the override keyword (Overrides in Visual Basic), you must explicitly call the base method. For example, if you override the OnLoad method on the page, you must call base.Load (MyBase.Load in Visual Basic) in order for the base implementation to be run.

Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
  • It's funny how having to bubble the event explicitly is always seen as unnecessary, error-prone and tedious. Some people might argue that overriding OnLoad (and calling base.OnLoad) adds compile-time safety and clarity to the application. :) – bzlm Jan 06 '10 at 14:22
1

In the page directive it says: Autoeventwireup="true"

Thats what happens, its automatically wired up to the Load event... (and some other events like PreInit, Init, Unload etc.)

Arjan Einbu
  • 13,543
  • 2
  • 56
  • 59