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?
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.
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.
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.
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; }
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With