Reading directory
I. Event Overview
Ii. Events in ASP. NET
Iii. Instances
Iv. Running Effect
I. Event Overview
When a Windows operating system is started, it needs to load hardware, hardware drivers, and software. After it is started, as long as you do not click it, it will not respond, when you click the "Start" menu, a menu will pop up. Most programs are event-oriented. What is event-driven? It is triggered by the outside, and an event is a signal that notifies the application of an important situation.
Steps for using events in C #
1: Create a delegate
2: Create an event to associate the created event or specific event with the created delegate.
3: Write an event handler
4: generate a delegate instance using the compiled event handler
5: add the delegated instance to the event list that generates the event object. This process is called subscription event.
Ii. Events in ASP. NET
ASP. NET supports three event rent events.
2.1: automatically generated pages in ASP. NET
For example, when a page is loaded
Protected void Page_Load (object sender, EventArgs e)
{
}
2.2: events that occur when a user interacts with a page
For example, click a button on the page, that is, some of your own events in the Web server control.
Protected void btnAdd_Click (object sender, EventArgs e)
{
}
2.3: html internal events, which are executed on the browser
Is the events implemented by javascript.
You can add two events to all Web server-side controls, client events, server-side events, and client events that are interpreted and executed by the browser. server-side events must be uploaded to the server for execution.
Iii. Instances
1 // Step 1 create a delegate 2 public delegate void MessageHandler (string message); 3 class Program 4 {5 static void Main (string [] args) 6 {7 Connection conn = new Connection (); 8 Display display = new Display (); 9 // step4' new MessageHandler (display. diaplayMessage) 'indicates to generate a delegate instance using the event handler.
// Step 5 '+ =' indicates the event reservation, 'conn. messageArrived indicates what to add to the event list that generates event objects. In combination, this delegated instance is added to the event list that generates event objects. This process is called subscription event, the delegate is the proxy. We can understand that the delegate is to associate an event processing function with an event.
10 conn. MessageArrived + = new MessageHandler (display. DiaplayMessage); 11 conn. Connect ();
12 Console. ReadKey ();
13}
14 15 public class Display16 {17 public string strMessage; 18 public Display () 19 {20 21} 22 // <summary> 23 // step3 write the event handler 24 /// </summary> 25 /// <param name = "message"> </param> 26 public void DiaplayMessage (string message) 27 {28 strMessage = message; 29 Console. writeLine ("message arrival: {0}", strMessage); 30} 31} 32} 33 34 public class Connection35 {36 // Step 2 create an event named 'messagearrived ', associate the created event or specific event with the created delegate. 37 public event MessageHandler MessageArrived; 38 private Timer polltimer; // declare a timer39 public Connection () 40 {41 polltimer = new Timer (100 ); // One hundred Ms 42 // loss event 43 polltimer. elapsed + = new ElapsedEventHandler (CheckForMessage); 44} 45 46 public void Connect () 47 {48 polltimer. start (); 49} 50 51 // <summary> 52 // 53 // </summary> 54 // <param name = "sender"> </param> 55/ // <param name = "e"> </param> 56 public Void CheckForMessage (object sender, ElapsedEventArgs e) 57 {58 Random random = new Random (); 59 // If the Random number is exactly 9 during time loss, we will output a message 60 if (random. next (9) = 0) & (MessageArrived! = Null) 61 {62 // MessageArrived ("hello") actually executed the DiaplayMessage () method 63 MessageArrived ("hello"); 64} 65} 66}
Iv. Running Effect