Why does Visual Studio give you the default Page_Load value for WebForms?

When creating a new WebForm, Visual Studios creates the Page_Load handler in the default code, which is very cool. Therefore, for many years I have always been adding code to do things like the specified properties of controls in Page_Load. I recently used Reflector to view some assemblies written by Microsoft, and saw that they applied the same type of logic in the OnLoad method (which supposedly triggers a load event). So I started to wonder where it is best to set the properties of controls in OnLoad or Page_Load? Or in a completely different way? And if not Page_Load, why does Studio add this to the code for?

My last thought: Although I know that setting the logic in OnLoad works fine, I will probably stick with Page_Load because this is normal. I asked if I could really find out if I was missing something new after I started to see that OnLoad appears in other people's code. Thank you all for your thoughtful answers!

+3
source share
5 answers

Page_Load - autoeventwireup OnLoad. , , , K. Ode to Code, , - . , OnLoad, , - , , base.Onload, .

+3

OnLoad, , , Page_Load. , Page_Load.

, Page_Load.

+1

, OnX- - , X. . , , , , , , : 1. , , 2. .

, , , , , , null. , .

, Load, OnLoad.

+1

. . , OnLoad. base.OnLoad().

+1

, ASP.Net 1.1, , , :

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
        InitializeComponent();
        base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
        this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion

, VS InitializeComponent , - .

+1

All Articles