This time, we will explore the execution order of Page loading
BasePage
public class BasePage : Page { public string BaseName { get; set; } public BasePage() { // //TODO: add constructor logic here // } protected override void OnLoad(EventArgs e) { int i = 1; this.Load += Page_Load; base.OnLoad(e); i++; SearchData(); } protected void Page_Load(object sender, EventArgs e) { this.BaseName = "father load Event"; } protected override void OnInit(EventArgs e) { int i = 1; this.Init += Page_Init; base.OnInit(e); i++; } protected void Page_Init(object sender, EventArgs e) { this.BaseName = "father init Event"; } /// <summary> ///Get data /// </summary> public virtual void SearchData() { } }
public partial class TestPage : BasePage { public string Name { get; set; } protected void Page_Load(object sender, EventArgs e) { this.Name = "Son load Event"; } protected override void OnLoad(EventArgs e) { int i = 1; base.OnLoad(e); i++; } protected override void OnInit(EventArgs e) { int i = 1; //this.Init += Page_Init; base.OnInit(e); i++; } protected void Page_Init(object sender, EventArgs e) { this.BaseName = "Son init Event"; } public override void SearchData() { this.Name = "implement"; } }
Every method body runs with breakpoints
Parent class constructor subclass constructor subclass OnInit subclass base.OnInit(e); parent class OnInit subclass base.OnInit(e); subclass page init (object sender, EventArgs E) or parent class page init (object sender, EventArgs E) events execute successively; parent class OnInit residual code execute complete subclass OnInit residual code execute complete subclass OnLoad subclass base.o Nload (E); "" parent OnLoad "" parent base.OnLoad(e); "" subclass Page_Load(object sender, EventArgs e) or parent Page_Load(object sender, EventArgs e) events execute successively "" parent OnLoad "subclass OnLoad" residual code execute complete execute subclass Page_Init(object sender, EventArgs e) or parent Page_Init(object sender, EventArgs e) " The order of the registered events is determined by the order of the registered events. If the events are registered in the parent constructor, the events of the parent class will be executed first.
public class BasePage : Page { public string BaseName { get; set; } public BasePage() { this.Load += Page_Load; this.BaseName = "father"; // //TODO: add constructor logic here // }
Another little knowledge is that we can see it on the page
AutoEventWireup="true" such a setting
This is to automatically register the page load event or page init event when the page is initialized. So if the registered event of the annotation is uncommented, the page init event of the subclass will execute on both sides, because the event is registered twice.