- 热门文章:
- · Server.Transfer,Response.Redirect 和 Page.Navigate 的区别
- · ASP.NET发送ICQ消息DIY
- · web页面用水晶报表的例子
- · Creating Custom Portal Modules
- · 几个.net的重要问题
- · SQL命令中DateTime格式参考
- · 关于webcontrol和pagelet的一点看法
- · 关于.net的几个重要问题的bigeagle版本
- · 关于datagrid的使用以及动态修改,以及使用存储过程的介绍
- · 转阿土伯推荐的文章:在 VS.NET 中编写 Web 应用程序(附图)(推荐)
- · 转新技术网:在ASP.NET中使用.NET组件
- · 自己写的一个资料验证的asp.net程序,大家看看吧!
一个实现自定义event的文章。。。我还没有完全摸透。。不知道有没人有兴趣。。新手就不用看了,先学会走...
In this discussion we will examine the steps to create a custom event that will alert the user or process that event has taken place under a specified condition. To understand how to setup the code structure to raise and handle events, an overview of the event architecture is needed.
Event Architecture
Below are a number of key points that encompass the .Net Framework event model.
- Events taking place in the .NET Framework are based upon the delegate model.
- The delegate class is the mechanism that allows the event sender to communicate with the event handler.
- An event sender can broadcast an event to a number of event handlers.
- An event handler must be registered with an event sender.
The fundamental definition of an event is: An event is a message that has been sent by an object due to some occurrence of an action within the workflow of the application. This action could be a button that has been clicked or specified timeout parameter value has been reached. This action triggers the event and the subsequent method that handles the event is initiated and the event is handled.
In the .NET framework, the sending object does not know what method will handle the event. Therefore, an agent must be utilized to handle the communication. This agent is the delegate class that holds a reference to the method that handles the event. The delegate class has an intrinsic signature and can only hold references to methods that obtain the same signature. You can equivocate the delegate class to a type-safe pointer to a callback. The event delegate has two parameters; these are the sending object and the returned data. Below in Listing 1 is generic declaration of the event delegate class.
Listing 1
Public delegate void EventHandler(object Sender, EventArgs e);
In order to initiate and handle custom events, event functionality needs to be provided. The fundamental structure is listed as follows:
- A class to hold the event data. This must be inherited from System.EventArgs.
- A delegate for the event.
- A class that raises the event.
- A class with a method that handles the event.
Now that we have discussed the fundamentals of event handling, we need a simple application to depict the event functionality.
Custom Events Application
The Custom Events Application is comprised of a simple Web Form that displays the event data message. This is shown in Figure 1.
Figure 1
In order to initiate some action that shows the functionality of event handling, a loop is created to count to some number and stop. During this count, a specific value is interrogated within the loop. When the specified value is equal to the loop value, the event is fired. In this case the value is 400000. The loop runs from 1 to 500000. When the value 400000 is reached the OnMessage method is called and the event is raised. Note: The InitEvent() method is called from Page_Load() method when the page is requested. The InitEvent() method is depicted in Listing 2.
Listing 2
private void InitEvent()
{
MessagingHandler mh = new MessagingHandler();
MessageEvent me = new MessageEvent();
me.Message += new MessageHandler(mh.GetMessageData);
for (int i = 1; i <= 500000; i++)
{
if (i == 400000)
{
MessageEventArgs e = new MessageEventArgs();
me.OnMessage(e);
lblMessage.Text = mh.EventMessage;
}
}
}
As you can see this is certainly overkill to display a line of text in a label field on a Web page. However, it is useful to examine the workflow of an event that is captured and handled in the .NET Framework. In order to make sense of the code fragment in Listing 2, we need to examine the classes that comprise the event structure. Below in Listing 3 shows the respective components that will raise and handle the event as it transpires.
Listing 3
public class MessageEventArgs : EventArgs
{
public string MessageText
{
get
{
return ("There has been 400000 values processed.");
}
}
}
public delegate void MessageHandler(object sender, MessageEventArgs e);
public class MessageEvent
{
public event MessageHandler Message;
public void OnMessage(MessageEventArgs e)
{
if (Message != null)
{
Message(this, e);
}
}
}
public class MessagingHandler
{
private string strMessage;
public string EventMessage
{
get
{
return strMessage;
}
set
{
strMessage = value;
}
}
public void GetMessageData(object sender, MessageEventArgs e)
{
string strMessage = e.MessageText;
EventMessage = strMessage;
}
}
The class MessageEventArgs will supply the data for event. Notice that it is inherited from EventArgs. As you can see MessageText is a property of the class and a get statement is utilized to return the data string. The delegate in this case is the MessageHandler. It has the sender object and the MessageEventArgs for the data in its argument list. Moving onto the class MessageEvent. This class will raise the event with the OnMessage() method by invoking the delegates. The MessagingHandler class will extract the event message from the MessageEventArgs and populate the EventMessage property so the message can be displayed on the Web page.
In summarization, we have a class that supplies the data, a delegate to provide the communication, a class to raise the event, and a class to handle the event. The basic workflow is as follows:
- The InitEvent() is initiated from the Page_Load() method when the page is requested.
- MessagingHandler mh = new MessagingHandler() instantiates the MessagingHandler class - Event Receiver.
- MessageEvent me = new MessageEvent() instantiates the MessageEvent class - Event Sender.
- me.Message += new MessageHandler(mh.GetMessageData) registers the event handler with the event source so that the MessagingHandler instance can receive alarm events. The += assignment operator adds the delegate to the list of delegates that are registered with the Message event.
- When the specified value is reached, MessageEventArgs e = new MessageEventArgs() is instantiated and the OnMessage() is called to raise the event.
- The OnMessage() method invokes the delegate which calls the GetMessageData() method to handle the event.
- The GetMessageData() method obtains the message text from the MessageEventArgs and populates the EventMessage property with the string data.
- The label field is populated with the EventMessage string property value and displayed on the Web page.
Run this example
[ run it ]
Conclusion
Since Web based applications are disconnected in nature and the handling of events are typically server-side the event message is displayed after the entire code sequence has transpired. This paradigm is not conducive for asynchronous processing due to the fire-and-forget processing. However, they can be useful if you are doing heavy server-side processing and validation before rendering the page.
The .NET Framework is providing the application developer a great deal of options to generate meaningful solutions to the enterprise, be it Web based applications, Windows Client based applications, or Server based Services
相关文章:
- · 关于在ASP.NET 中进行调试的方法(转载自itpeople),不过我个人对第三招不以为然,有了vs.net还要那个...
- · 菜鸟入门篇---有关ASP.NET的一些基本说明,解释. [页面标识]
- · 我的第一个ASP+程序,如果是新手请进来看吧。谢绝高手。:)
- · Security Hole In ASP.NET Beta 1 (from angryCoder)
- · 关于从toolbox内拖放控件到form时出错的解决办法
- · XML、DataSet、DataGrid结合写成广告管理程序(上)(转载)
- · XML、DataSet、DataGrid结合写成广告管理程序(下)(转载)
- · 我的aspx为什么无法显示中文?
- · web窗口间的互相控制
- · BigEagle的数据库结构(转载,一动手,就轻拿5分)
- · 转雨天妹妹的文章:TreeView的DHTML实现(可以实现拖动效果哟)
- · 吐血奉献:如何搞定DataGrid 分栏的大小(即DataGrid的可视化控制).
- · Is your .NET Code safe?
- · Introduction to .NET Reflection
- · .net里面的数值格式变换
- · Picture Numeric Format Strings(我很难解释大家自己看)
- · 数值变换时的格式化字符举例
- · Numeric Parse Method
- · 日期和时间的转换
- · 自定义的转换格式
- · 如何将powerpoint转换为html
- · css设定表格宽度
- · .Net边学边讲(三)
- · Calling a Button Event from a Compiled DLL
- · datagrid怎么设置分页?
- · .NET之ASP Web Application快速入门(3)(转载)
- · .NET之ASP WebApplication快速入门(4)(转载)
- · .NET之ASP WebApplication快速入门(5)(转载)
- · .NET之ASP Web Application快速入门(1)(转载)
- · .NET之ASP Web Application快速入门(2) (转载)
- · ASP.NET中的错误处理支持
- · ASP.NET中的代码分离
- · 在ASP.NET中使用AdRotator控件(转)
- · 在ASP.NET中动态生成图形(转)
- · 用ASP.NET加密口令(转)
- · ASP.NET中的错误处理支持(转)
- · ASP.NET中发送Email完整实例(转)
- · ASP.NET中的注释符号
