Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What calls Page_Load and how does it do it?

Tags:

asp.net

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?

like image 943
Orion Adrian Avatar asked Sep 29 '09 19:09

Orion Adrian


People also ask

What is Page_Load?

Your web form has a method called Page_Load. This method is called each time your page is loaded. This includes: 1. The first time the page is loaded, e.g., when a user enters the url to it or clicks on a link to it.

What is a Page_Load event?

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.

What is the use of page load event in asp net?

Page_Load happens after ViewState and PostData is sent into all of your server side controls by ASP.NET controls being created on the page. Page_Init is the event fired prior to ViewState and PostData being reinstated. Page_Load is where you typically do any page wide initilization.

What is page load C#?

Page_Load() method is called after a preLoad event. With Page_Load() you can set default values or check for postBacks etc. protected void Page_Load(object sender, EventArgs e) { int x = 10; }


2 Answers

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.

like image 97
Andrew Hare Avatar answered Sep 24 '22 09:09

Andrew Hare


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.

like image 40
M4N Avatar answered Sep 24 '22 09:09

M4N