Base Class for Code-Behind Problem
Hereâs a paragraph from the solution of Chapter 3
âOnInit (shown below) is an important method. What we're doing with our override is calling the OnInit method in the base class (System. Web.UI. Page) and then adding our custom page-load event handler (PhilePage_Load) to the Load delegate holder:â
I tried to write the same code but I have a problem. First hereâs the code that I wrote:
I wrote a class called Basepage.cs as following
///////////////////////
public class BasePage: System.Web.UI.Page
{
public BasePage()
{
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Load += new System.EventHandler(this.BasePage_Load);
}
private void BasePage_Load(object sender, System.EventArgs e)
{
Trace.Warn("Here a call to 'BasePage_Load' method of the base class");
}
}
//////////////
Then I created a new Web Form called WebForm1.aspx and because I donât know how to make this form inherent from BasePage.cs I deleted the WebForm1.aspx.cs (the code behind) and changed the Page Directive to contain this
Codebehind="WebForm1.aspx.cs" and Inherits="test1.WebForm1"
And hereâs the WebForm1.aspx.cs (the code-Behind file that I inherent from BasePage)
////////////////////
public class WebForm1 : BasePage
{
private void Page_Load(object sender, System.EventArgs e)
{
Trace.Warn("Here call to 'page_Load' of the derived class");
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
So the problem is that when I trace this page I found that Page_Load method come first and load before BasePage_Load method so why?????????????????
In thePhile It was normal and PhilePage_Load load first and then Page_Load (of the code-behind) so whatâs the problem in my code??????????
Thanks
Marenela
|