What calls Page_Load and how does it do it?

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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.

Method 3

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 WindowsMicrosoft.NetFrameworkv*Temporary ASP.Net Files.

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

Method 4

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.

Method 5

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x