I have a base control:
public partial class BaseControl : System.Web.UI.UserControl
{
protected virtual void Page_Load(object sender, EventArgs e)
{
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Page.LoadComplete += Page_LoadComplete;
}
protected virtual void Page_LoadComplete(object sender, EventArgs e)
{
Page.LoadComplete -= Page_LoadComplete;
}
}
I then have a control that derives from this
public partial class MyChildControl : BaseControl
The problem I have is that if I load MyChildControl more than once then I get an object ref error on Page.LoadComplete -= Page_LoadComplete; in BaseControl. I sort of understand why but I have 2 questions:
- Shouldn’t each
MyChildControlreference it’s own version ofBaseControlwhich in turn registers it’s ownPageLoadComplete? - How can I check to see if it is not null? I can’t seem to do
!= null.
EDIT: It seems that Page is null and checking Page != null resolves the issue but I still don’t understand how Page is null.
Page_LoadCompletecan’t be null, it’s just a delegate created for methogPage_LoadComplete. So i think Page inPage.LoadCompletemay be null or page has custom logic for adding handler to event LoadComplete and has ref error there