将委托或事件属性添加到此类?

时间:2010-10-04 13:56:45

标签: c# .net asp.net events delegates

我创建了一个控件,其他开发人员可以创建一个实例并使用。控件中有一个按钮单击。如何允许开发人员在我的控件的某个部分插入自己的代码?我不确定在这种情况下是否或如何使用委托或事件。以下示例中有人可以提供帮助:

public class MyControl : CompositeControl
{
   ...
   void btnSubmit_Click(object sender, EventArgs e)
   {
      //DO SOMETHING
      if (success)
      {
         //CALL DEVELOPER'S LOGIC
      }
   }
}

在开发人员代码中,当按钮单击控件成功时,他们如何传递逻辑?

protected override void CreateChildControls()
{
    var ctlMyControl = new MyControl();

    //ADD SuccessCode() TO ctlMyControl
    //SO IT IS TRIGGERED ON SUCCESS
    //MAYBE SOMETHING LIKE:
    //ctlMyControl.SuccessEvent = SuccessCode()??

    this.Control.Add(ctlMyControl);
}

protected void SuccessCode()
{
    //DO SOMETHING
}

如何更新MyControl以允许此操作?

4 个答案:

答案 0 :(得分:5)

您需要向控件添加event,如下所示:

void btnSubmit_Click(object sender, EventArgs e) {
    //DO SOMETHING
    if (success) {
        OnSomethingCompleted();
    }
}

///<summary>Occurs when the operation is successfully completed.</summary>
public event EventHandler SomethingCompleted;
///<summary>Raises the SomethingCompleted event.</summary>
internal protected virtual void OnSomethingCompleted() { OnSomethingCompleted(EventArgs.Empty); }
///<summary>Raises the SomethingCompleted event.</summary>
///<param name="e">An EventArgs object that provides the event data.</param>
internal protected virtual void OnSomethingCompleted(EventArgs e) {
    if (SomethingCompleted != null)
        SomethingCompleted(this, e);
}

在表单中,您可以为事件添加处理程序,如下所示:

myControl.SomethingCompleted += myControl_SomethingCompleted;

void myControl_SomethingCompleted(object sender, EventArgs e) {
    //Do things
}

有关详细信息,请参阅documentation

答案 1 :(得分:1)

由于您的事件使用EventHandler类型的默认签名,因此您只需要公开EventHandler类型的公共事件。所以看看下面的代码。

以下是您实施新活动的方式!

public class MyControl : CompositeControl
{
   //New public event declaration of type EventHandler
   public event EventHandler Submit_OnClick;

   void btnSubmit_Click(object sender, EventArgs e)
   {
      //DO SOMETHING
      if (success)
      {
         //CALL DEVELOPER'S LOGIC

         //Code to raise the event only if there are subscribers.
         if (Submit_OnClick != null)
         {
           Submit_OnClick(this, new EventArgs());
         }
      }
   }
}

以下是您使用新活动的方式!

protected override void CreateChildControls()
{
    var ctlMyControl = new MyControl();
      //Subscribed to the newly added event of your MyControl class
        ctlMyControl.Submit_OnClick += new EventHandler(SuccessCode);

    //ADD SuccessCode() TO ctlMyControl
    //SO IT IS TRIGGERED ON SUCCESS
    //MAYBE SOMETHING LIKE:
    //ctlMyControl.SuccessEvent = SuccessCode()??

    this.Control.Add(ctlMyControl);
}

//Modified the signature to be compliant with EventHandler type
protected void SuccessCode(object sender, EventArgs e)
{
    //DO SOMETHING
}

享受!

答案 2 :(得分:0)

您需要声明该事件,并在必要时创建一个自定义事件args类,以传递有关该事件的信息。

这是a blog post我写了一段时间,介绍了整个过程。

答案 3 :(得分:0)

您应该向您的控件添加一个事件。

这样的事情:

public class MyControl : CompositeControl
{
       public event EventHandler Success;
       ...
       void btnSubmit_Click(object sender, EventArgs e)
   {   
       //DO SOMETHING
        if (success)
        {
             //CALL DEVELOPER'S LOGIC
             OnSuccess();
        }
   }

    private static void OnSuccess()
    {
         if(Success != null)
         {
              Success(this, EventArgs.Empty); //or you can pass event args you want. But you should use EventHandler<T> instead.
         }
    }
}

与其他开发者相比,你可以这样控制:

protected override void CreateChildControls()
{
    var ctlMyControl = new MyControl();

    ctlMyConstrol += SuccessCode;

    this.Control.Add(ctlMyControl);
}

protected void SuccessCode(object sender, EventArgs e)
{
    //DO SOMETHING
}

您可以在MSDN上阅读有关活动的更多信息。