Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用户控制事件_C#_Asp.net_User Controls_Event Handling - Fatal编程技术网

C# 用户控制事件

C# 用户控制事件,c#,asp.net,user-controls,event-handling,C#,Asp.net,User Controls,Event Handling,我有一个用户控件,其中有一个名为upload的按钮。按钮单击事件如下所示: protected void btnUpload_Click(object sender, EventArgs e) { // Upload the files to the server } 在用户控件所在的页面上,在用户单击上载按钮后,我想在用户控件中执行按钮单击事件代码之后立即执行一些操作。单击事件完成工作后,如何点击该事件?您必须在用户控件中创建一个事件,例如: public event EventHan

我有一个用户控件,其中有一个名为upload的按钮。按钮单击事件如下所示:

 protected void btnUpload_Click(object sender, EventArgs e)
{
  // Upload the files to the server
}

在用户控件所在的页面上,在用户单击上载按钮后,我想在用户控件中执行按钮单击事件代码之后立即执行一些操作。单击事件完成工作后,如何点击该事件?

您必须在用户控件中创建一个事件,例如:

public event EventHandler ButtonClicked;
然后在你的方法中发射事件

protected void btnUpload_Click(object sender, EventArgs e)
{
   // Upload the files to the server

   if(ButtonClicked!=null)
      ButtonClicked(this,e);
}

然后,您将能够附加到用户控件的ButtonClicked事件。

在UserControl的CodeBehind中创建公共属性:

    public Button btn
    {
        get { return this.Button1; }
    }
然后,在页面加载中,您可以像这样使用它:

    WebUserControl11.btn.Click += (s, ea) => { Response.Write("Page Write"); };

您可以使用显式事件实现将事件直接连接在一起,而不是编写事件处理程序来调用另一个事件:

public event EventHandler ButtonClicked
{
   add { btnUpload.Click += value; }
   remove { btnUpload.Click -= value; }
}

现在,订阅您的
按钮点击
事件的任何人实际上都是直接订阅
btnUpload
控件的
点击
事件。我发现这是一种更简洁的实现方法。

然后确保包含usercontrol的页面正在侦听事件。。。。this.myUploadControl.ButtonClicked+=新事件处理程序(myUploadControl\u ButtonClicked);其中MyUploadControl_ButtonClicked在Page类中声明为事件处理程序。