Difference OnInit and OnLoad in ASP.NET?

I had an interview a week ago and one of the questions was what the difference was between OnInit and Onload in ASP.NET? I had no clue and I don’t find any simple answers on the net so can someone explain shortly and simple what the difference is between both? (What I found was that the difference was somehting in the lifecycle).

difference between Page_Init vs OnInit

I had an interview a week ago and one of the questions was what the difference between OnInit, Page_Initand and PreRender. and which one is preferable.

Difference between onload() and $.ready?

Can you list the difference between onload() and $(document).ready(function(){..}) functions in the using jQuery?

Difference between page_load and onLoad

What is difference between page_load and onLoad functions in ASP.NET codebehind?

Adding/removing session variables on Page OnInit/OnLoad in C#

I am using C#. I am having below code in C#: protected override void OnInit(EventArgs e) { try { if (Session[boolSignOn].ToString() == true.ToString()) { lblPanelOpen.Text = Session[panelOpen].T

Difference between OnLoad method and Load event?

What is the difference between OnLoad method and Load event? I am developing WinForm controls. Should I register to Load event or override the OnLoad method? What are the advantages and the disadvanta

wxWidgets OnInit

This code works fine: #include <wx/wx.h> class MyApp : public wxApp { virtual bool OnInit(); }; IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { wxFrame *frame = new wxFrame(NULL, -1, _(Hello World

Difference between onLoad and ng-init in angular

I am learning angular. I don’t understand what is difference between onLoad and ng-init for initialization of a varibale. In which scope it creates this variable. For example <ng-include onLoad=se

Difference between window.onload = function () vs window.onload=

Hello I have an aspx document, at the bottom of the page I have this code: <script language=javascript type=text/javascript> window.onload = migrate(); </script> It works well, but i

What’s the difference between “aspnet_regiis -ir” and “aspnet_regiis -iru”?

The help text of aspnet_regiis utility says the following for -ir option: Install this version of ASP.NET, register only. Do not change any web applications to use this version. And the following f

difference between pageLoad , onload & $(document).ready()

i need to know in more detail of what is the differences between pageLoad , onload & $(document).ready() I found answer but that is not ver clear to me. the answer is like The ready event occurs

Answers

You need to read up on the ASP.NET page lifecycle.

OnInit happens earlier in the lifecycle – view state changes have not been done yet and tracking of it has not been turned on.

Page_Init is raised before Page_Load. Page_Init is a good place for code that you want executed before you process further such as attaching event handlers to the load event.

it is better not to access controls in this event because you aren’t guaranteed they have been created.

The Page_Load is a good place to store code where you initialize values and any controls specific to the page because you know at this point the controls exist and are available.

You will place a lot more code in Page_Load than you will in Page_Init for the majority of your apps

Both these methods of Control class are invoked by ASP.NET. OnInit() method raises the Init event and OnLoad() method raises the Load event.

OnInit (the Init event) happens after all controls have been initialized, but before ViewState tracking is enabled. It’s called bottom-up (the Init events for child controls are called before their parent’s Init event).

Init is a good place to add dynamic controls to your page or user control (though it’s not a requirement). If you can, then those controls will have their ViewState restored automatically during postbacks (see below). It’s a risky place to set control properties, though, because they can be overwritten by incoming ViewState. Init is the right place to set ViewStateUserKey, which can help protect your site from one-click attacks. You would also call RegisterRequiresControlState() from there, if you’re using control state.

Right after the Init event, each control enables ViewState tracking, so any changes to a control’s properties after that will be reflected in ViewState.

The next events at the page level are InitComplete and PreLoad, neither of which is visible at the control level. During a postback, incoming ViewState is restored into controls between InitComplete and PreLoad.

Then comes the Load event, which happens for both controls and the page. Load is called first at the parent level, and then for any child controls. A master page behaves like a control on a page with regard to event ordering.