日期:2011-02-25 浏览次数:20477 次
The latest offering from Microsoft to support software development is the .NET Framework. Inside this vast Framework is the ASP.NET. The ASP.NET facilitates the application development community in generating high performance Web based applications that can be data rich and work in a heterogeneous environment. 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++)<
免责声明: 本文仅代表作者个人观点,与爱易网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
|