在用户控件asp.net中声明事件

本文关键字:声明 事件 net asp 用户 控件 | 更新日期: 2023-09-27 18:01:00

我已经在我的用户控件上声明了一个事件

public event EventHandler<AddressEventArgs> SaveButtonClick; 
protected void ButtonSave_Click(object sender, EventArgs e) 
{ 
  if (SaveButtonClick != null) 
  { 
    SaveButtonClick(this, new AddressEventArgs) ; 
  } 
}

在我将用户控件添加到新页面后,我将如何捕获用户控件引发的事件?

在用户控件asp.net中声明事件

您可以对事件使用[Browsable]属性,也可以强制绑定到事件。

userControl.SaveButtonClick += new EventHandler(handlerFunctionName);
public void handlerFunctionName(AddressEventArgs args)
{
     // Here, you have access to args in response the the event of your user control
}
Control.SaveButtonClick += controlClicked;
protected void controlClicked(object sender, EventArgs e) 
{ 
   //Do work
}

首先,您需要订阅事件,并在引发事件时为其提供一个调用方法。