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

时间:2011-07-04 17:43:14

标签: c# asp.net visual-studio

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

public event EventHandler<AddressEventArgs> SaveButtonClick; 

protected void ButtonSave_Click(object sender, EventArgs e) 
{ 
  if (SaveButtonClick != null) 
  { 
    SaveButtonClick(this, new AddressEventArgs) ; 

  } 
}

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

2 个答案:

答案 0 :(得分:4)

您可以[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
}

答案 1 :(得分:0)

Control.SaveButtonClick += controlClicked;

protected void controlClicked(object sender, EventArgs e) 
{ 
   //Do work
}

首先,您需要订阅您的活动,并为其提供一个方法,以便在引发活动时进行调用。