This article illustrates how to implement C # custom events in asp.net. Share to everyone for your reference. The specific analysis is as follows:
C # Custom events are divided into six steps, declaring the delegate about the event, declaring the event, writing the function that raised the event, and handling the event, here we will give a concrete introduction.
The specific implementation steps for the C # custom event are as follows:
1, declare a delegate: (for the definition of the type of event)
Such as:
Copy Code code as follows:
Public delegate void event name EventHandler (Object Serder, EventArgs e);
The event name is replaced with your own, followed by the EventHandler of C #, and of course if you do not want to comply, you can use any character or even not.
If you want to customize the parameter EventArgs of an event, you can derive your own event argument class from this class, and then in delegate's declaration, replace EventArgs with your argument class
Note: To fully understand the rationale of custom events, you need to learn about delegate.
2. Declare an event in your class and use the delegate of step 1 as the type of event:
Public event event name EventHandler event name;
3, in your class need to trigger the event in the method, add event trigger code :
Event name (this, new EventArgs ());
Or:
if (event name!=null) event name (this, new EventArgs ()); If you use your own event parameter class, you can replace the new EventArgs () with your argument class case and save the data you need to pass in your parameter class.
4. C # Custom Event registration:
Event registration is no different from a normal event registration, which means that if an external object needs to respond when your event is triggered, you can register the event in the external constructor (or where appropriate)
class instance with events. Event name + = new event name EventHandler (event-handling method name);
5, write Event processing methods:
public void Event-handling method name (object sender, EventArgs e) {
Add your code
}
Note: If you handle your own triggering events in a class, you can choose the way of C # custom event steps 4 and 5, which is to register yourself, or you can invoke the event handling method directly in the triggering event code.
Now, let's take a look at an example.
Copy Code code as follows:
Namespace Custom Events
{
Custom events are grouped into the following steps:
Class Program
{
static void Main (string[] args)
{
Sixth step: main function call
Dog Dog = new Dog ();
Host host = new host (dog);
DateTime now = new DateTime (2013,8,31,22,39,50);
DateTime nigth = new DateTime (2013,8,31,22,40,0);
Console.WriteLine ("~~~~~~~~~ time to start walking ah ~~~~~~~~~");
while (now <nigth) {
Console.WriteLine ("Current Time:" +now);
System.Threading.Thread.Sleep (1000);
now = Now. AddSeconds (1);
}
Console.WriteLine ("~~~~~~~ I am a thief, I came to ~~~~~~~~~~~~");
Dog. Onalarn ();
}
}
Class Dog {
First step: Declare a delegate about an event
public delegate void Alarneven (Object Sender,eventargs e);
Step two: Declare the event
public event Alarneven Alarn;
Step three: Write the function that raises the event
public void Onalarn () {
if (this. Alarn!=null) {
Console.WriteLine ("N Dog alarm: A thief came in." Wang ~~~~~~~~~ ");
This. Alarn (This,new EventArgs ());
}
}
}
Class Host {
IV: Writing handlers for events
void Hostalarn (object sender, EventArgs e)
{Console.WriteLine ("Master: Catch the Thief!.");}
Step Fifth: Register the handlers for the event
Public Host (Dog Dog) {
Dog. Alarn + = new Dog.alarneven (Hostalarn);
}
}
}
I hope this article will help you with the ASP.net program design.