C# Basecontrol Page.LoadComplete-=Page_LoadComplete在多次加载子控件时引发对象引用错误

C# Basecontrol Page.LoadComplete-=Page_LoadComplete在多次加载子控件时引发对象引用错误,c#,asp.net,events,event-handling,C#,Asp.net,Events,Event Handling,我有一个基本控件: 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.LoadComp

我有一个基本控件:

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;
    }
}
然后我有一个从中派生的控件

public partial class MyChildControl : BaseControl
我遇到的问题是,如果我加载
MyChildControl
多次,那么在
Page.LoadComplete-=Page\u LoadComplete上就会出现对象引用错误
BaseControl
中。我有点理解为什么,但我有两个问题:

  • 每个
    MyChildControl
    是否都应该引用自己版本的
    BaseControl
    ,后者反过来注册自己的
    PageLoadComplete
  • 如何检查它是否为空?我似乎不能做
    !=空

  • 编辑:似乎
    Page
    为空,正在检查
    Page!=null
    解决了这个问题,但我仍然不明白
    页面
    是如何为null的。

    Page\u LoadComplete
    不能为null,它只是为methodg
    Page\u LoadComplete
    创建的一个委托。所以我认为,
    Page.LoadComplete
    中的页面可能为null,或者页面具有自定义逻辑,用于将处理程序添加到事件LoadComplete,并且其中存在ref error

    Page\u LoadComplete
    不能为null,它只是为方法页面\u LoadComplete创建的委托。因此,我认为
    Page.LoadComplete
    中的页面可能为空,或者页面具有自定义逻辑,用于将处理程序添加到事件LoadComplete,并且在那里存在ref error。好的,检查
    Page!=null
    似乎对其进行排序。我只能假设我在某个地方将页面置零。。我会看的。@Nikolay-你能把你的评论作为回答吗。这解决了我的问题,尽管让我有点困惑:)