I have a class named PageBase inheriting from ASP.NET Page. I want to do something in PageBase‘s Page_Load. All pages in my application have their logic done on their Page_Load.
I’m thinking of a way that Page_Load of both of PageBase and other pages run without having to override Page_Load in pages. Is it possbile?
I have a class named PageBase inheriting from ASP.NET Page . I want to
Share
Yes, it’s possible. Just attach the Page_Load handler within constructor. This is how you do it.
EDIT
Here’s how it works.
Loadis a public event declared inSystem.Web.UI.Pagewhich is inherited in the PageBase class as it is. Since this is an event you can attach as many as handler on the event, each of them will be called/executed whenever theLoadevent is raised. In thePageBaseclass we are attaching an event-handler routine to the Load event. This is better way to do it instead of inheriting the OnLoad protected routine where you need to explicitly call the base version. When the actual Page object is created/instantiated the base class object is created first then the sub-class is instantiated, so when the PageBase object is constructed the event-handler is attached onto the Load event which will be called whenever the Page object fires the Load event.