An event is a message. If data needs to be transmitted, the event is data and exposed data.
Ipostbackeventhandler
Ipostbackdatahandler
Sending event and notification data is only half of the work, and the other half is that the client registers the event and its related data. The client must register the accepted event.
Implement registration through. Net delegation. Send events using delegation.
Event Data class name event name and keyword eventargs
Event data requires eventargs
The delegate name will contain eventhandler
Define events with delegated instance and keyword event
Public event visitedataeventhandler gvisite;
Implement Delegation
Encapsulate this method with Delegation
Event delegates are a subset of multicast delegates, including an inline list called call lists.
This. gvisite + = mydelegate;
A protected virtual method is required to call the delegate to send the notification to the client.
Event optimization:
Define a delegate event as a public field. 1 will waste system resources (each reference is generated separately) 2. Ensure thread security (most do not use threads) the ADD and move methods generated by each event Delegate field generate unnecessary overhead (locked first ).
The eventhandlerlist class solves these two performance problems.
Define an event as a Private Static Property
Private Static readonly object validcricardeventkey = new object ();
Define the event as an attribute, and replace the get set with add and remove.
The call method is (eventhandlerlist events)
Event. addhandler
Event. removehandler
Event [validcricardeventkey]
Notify the server control by including the page.
Include page
Returns the name attribute value of the HTML element that is returned.
To search for the uniqueid of the preceding control.
Checks whether the ipostbackeventhandler interface is implemented, and exposes the raisepostbackevent method.
Name of the interface used to restrict methods and attributes
Do not use any Modifier
Call protected Virtual Methods
Void ipostbackeventhandler. risepostbackevent (string S)
{
This. risepostbackevent (s );
}
Return data
Ipostbackdatahandler -- "loadpostdata
Bool ipostbackdatahandler. loadpostdata (string postdatakey, namevaluecollection values)
{
Return this. loadpostdata (postdatakey, values );
}
In addition, the raisepostdataeventchanged method is implemented.
Protected virtual void raisepostdataeventchanged ()
{
This. raisepostdataeventchanged ();
}
Declare Event Mode:
1 namespace customcomponents
2 {
3 Public class creditcardform6: creditcardform5, ipostbackeventhandler,
4 ipostbackdatahandler
5 {
6 Private Static readonly object paymentmethodchangedeventkey = new object ();
7 public event eventhandler paymentmethodchanged
8 {
9add {events. addhandler (paymentmethodchangedeventkey, value );}
10 remove {events. removehandler (paymentmethodchangedeventkey, value );}
11}
12 protected virtual void onpaymentmethodchanged (eventargs E)
13 {
14 eventhandler handler = events [paymentmethodchangedeventkey] As eventhandler;
15if (handler! = NULL)
16 handler (this, e );
17}
18 Private Static readonly object creditcardnochangedeventkey = new object ();
19 public event eventhandler creditcardnochanged
20 {
21add {events. addhandler (creditcardnochangedeventkey, value );}
22 remove {events. removehandler (creditcardnochangedeventkey, value );}
23}
24 protected virtual void oncreditcardnochanged (eventargs E)
25 {
26 eventhandler handler = events [creditcardnochangedeventkey] As eventhandler;
27if (handler! = NULL)
28 handler (this, e );
29}
30 Private Static readonly object cardholdernamechangedeventkey = new object ();
31 public event eventhandler cardholdernamechanged
32 {
33add {events. addhandler (cardholdernamechangedeventkey, value );}
34 remove {events. removehandler (cardholdernamechangedeventkey, value );}
35}
36 protected virtual void oncardholdernamechanged (eventargs E)
37 {
38 eventhandler handler = events [cardholdernamechangedeventkey] As eventhandler;
39if (handler! = NULL)
40 handler (this, e );
41}
42 Private Static readonly object expirationdatechangedeventkey = new object ();
43 public event eventhandler expirationdatechanged
44 {
45add {events. addhandler (expirationdatechangedeventkey, value );}
46 remove {events. removehandler (expirationdatechangedeventkey, value );}
47}
48 protected virtual void onexpirationdatechanged (eventargs E)
49 {
50 eventhandler handler = events [expirationdatechangedeventkey] As eventhandler;
51if (handler! = NULL)
52 handler (this, e );
53}
54}
55}