Reputation: 10753
A ChildClass user control I'm working with has a chain of parent classes, ultimately inheriting from System.Web.UI.UserControl.
public BasestClass : AnotherClassExtendingUserControl
{
private void Page_Load(object sender, EventArgs e)
{
// do some stuff
}
}
public BaseClass : BasestClass
{
// no Page_Load
}
Two parent classes up the chain, Page_Load is marked private. The 'middle' class obviously doesn't override it, but I need to add something to the Page_Load of the ChildClass user control.
public ChildClass : BaseClass
{
// think I need to override Page_Load here,
// but there is no acceptable method to override
}
What can I do to get into the Page_Load of ChildClass? There are controls in that control that I need to manipulate that do not exist in BaseClass or BasestClass.
Upvotes: 2
Views: 1520
Reputation: 46067
The Page_Load
(or Load
) event gets raised during execution of the OnLoad
event method, so I would suggest moving your logic there. You shouldn't have a problem overriding the OnLoad
method from your child class.
Upvotes: 2
Reputation: 1878
I copied your classes and just changed two things:
BasestClass : System.Web.UI.UserControl
That shouldn't alter anything, since you said that BasestClass
ultimately inherits from UserControl
.
I also added this to BasestClass.Page_Load:
Response.Write("BasePage_Load");
Now, to solve your problem:
You can add a constructor to ChildClass and add an event handler for the Load event.
public ChildClass()
{
Load += new EventHandler(_Default_Load);
}
void _Default_Load(object sender, EventArgs e)
{
Response.Write("Event Handler");
}
You can also override OnPreRender:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Response.Write("OnPreRender");
}
Doing all of the above, this is output in the response:
Event Handler
BasePage_Load
OnPreRender
If you want to be sure that the Page_Load
method of BasestClass
has already run before your new code, OnPreRender
is the place to do your thing.
Upvotes: 1