简体   繁体   English

从动态添加的子控件分配委托事件处理程序

[英]Assign delegate event handler from dynamically added child control

I have a control that handles commenting. 我有一个处理评论的控件。 In this control, I have set a delegate event handler for sending an email. 在此控件中,我设置了一个用于发送电子邮件的委托事件处理程序。

I then have various types of controls, eg blog, articles etc, each of which may or may not have the commenting control added (which is done dynamically with me not knowing the id's), ie the commenting control is added outside this control. 然后,我有各种类型的控件,例如博客,文章等,每种控件都可以添加也可以不添加评论控件(这是我不知道ID的情况下动态完成的),即评论控件被添加到该控件之外。 Each of these controls handles it's emailing differently(hence the event). 这些控件中的每一个都以不同的方式处理电子邮件(因此发生事件)。

What I'm trying to determine, is how to assign the event in the parent control. 我要确定的是如何在父控件中分配事件。 At the moment, I'm having to recursively search through all the controls on the page until I find the comment control, and set it that way. 此刻,我必须递归搜索页面上的所有控件,直到找到注释控件,并以此方式进行设置。 Example below explains: 以下示例说明:

COMMENTING CONTROL 评论控制

public delegate void Commenting_OnSendEmail();

public partial class Commenting : UserControl
{
   public Commenting_OnSendEmail OnComment_SendEmail();

   private void Page_Load(object sender, EventArgs e)
   {
      if(OnComment_SendEmail != null)
      {
          OnComment_SendEmail();
      }
   }
}

PARENT CONTROL 家长控制

public partial class Blog : UserControl
{
   private void Page_Load(object sender, EventArgs e)
   {
      Commenting comControl = (Commenting)this.FindControl<Commenting>(this);
      if(comControl != null)
      {
         comCtrol.OnComment_SendEmail += new Commenting_OnSendMail(Blog_Comment_OnSendEmail);
      }
   }
}

Is there an easier way? 有更容易的方法吗?

EDIT: 编辑:

The reason I ask is that if I search from this.Page as the initial control, I am worried about time taken to search down the control tree to find it. 我问的原因是,如果我从this.Page作为初始控件进行搜索,我担心在搜索控件树以查找它时所花费的时间。 Each different type of page would be different in how many control it would have. 每种不同类型的页面在控制多少方面将有所不同。 On some testing, it returns back quite quickly the result. 在某些测试中,它会很快返回结果。

You could override the AddedControl event of your Blog class and check if the added control is instance of type Commenting. 您可以覆盖Blog类的AddedControl事件,并检查添加的控件是否为Commenting类型的实例。 Something like: 就像是:

public partial class Blog : UserControl { 

    protected override void AddedControl(Control control, int index) {
      base.AddedControl(control, index);

      Commenting commentingControl = control as Commenting;
      if (commentingControl == null) return;

      commentingControl.OnComment_SendEmail += new Commenting_OnSendMail(Blog_Comment_OnSendEmail);
    }
}

Of course, you can put this code on a base class of all your "commentable" user controls and have an abstract method to actually handle the event. 当然,您可以将此代码放在所有“可注释”用户控件的基类上,并具有一个抽象方法来实际处理该事件。

Just one thing: the AddControl event happens AFTER the Page_Load, so be careful. 只是一件事:AddControl事件在Page_Load之后发生,所以要小心。

Cheers, 干杯,

André 安德烈

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM