简体   繁体   English

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

[英]Declaring event in user control asp.net?

I have declared a event on my user control我已经在我的用户控件上声明了一个事件

public event EventHandler<AddressEventArgs> SaveButtonClick; 

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

  } 
}

After I have added the user control to the new page, how would I trap the event raised by the user control?将用户控件添加到新页面后,如何捕获用户控件引发的事件?

Either you can [Browsable] property on the event, or you can imperatively bind to the events.您可以在事件上使用[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
}

First you need to subscribe to your event, and give it a method to call when the event is raised.首先,您需要订阅您的事件,并为其提供一个在引发事件时调用的方法。

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

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