Che*_*hev 2 c# asp.net events user-controls webforms
我是webforms中用户控件的新手,我需要向控件添加一个事件,以便使用该控件的开发人员可以为此事件添加事件处理程序.这样做的最佳方法是什么?
此控件是自定义上载器控件.控件将文件异步上载到Web服务,并存储在隐藏字段中成功上载的文件列表.在下一篇文章中,我想从请求表单集合中的这个隐藏字段中读取,如果它不是null,那么我想触发一个成功的上传事件.
任何帮助深表感谢.谢谢!
对不起,如果我有点模糊.我正在寻找一个可以解雇的服务器端事件.我只是不熟悉创建它们.
首先,为您的事件参数创建一个类.
// this can house any kind of information you want to send back with the trigger
public class MyNewEventArgs : EventArgs { ... }
Run Code Online (Sandbox Code Playgroud)
接下来,在控件的类上创建事件.这是使用委托和事件本身完成的.
// event delegate handler
public delegate void MyNewEventHandler(object s, MyNewEventArgs e);
// your control class
public class MyControl : Control
{
// expose an event to attach to.
public event MyNewEventHandler MyNewEvent;
Run Code Online (Sandbox Code Playgroud)
接下来,您需要从代码中触发事件.我们通过抓住事件,检查订阅者然后触发来实现此目的.
// grab a copy of the subscriber list (to keep it thread safe)
var myEvent = this.MyNewEvent;
// check there are subscribers, and trigger if necessary
if (myEvent != null)
myEvent(this, new MyNewEventArgs());
Run Code Online (Sandbox Code Playgroud)
可以在MSDN上找到有关如何创建事件的更多信息.
| 归档时间: |
|
| 查看次数: |
8334 次 |
| 最近记录: |