Chapter 3 add events for controls
Now, we have developed a control before. The property is also added, and the development is also very standard, but the control is still the last point: add events.
SeriesArticleLink:
ASP. NET custom control component development chapter 1 to be continued
ASP. NET custom control component development chapter 1 Article 2 to be continued
ASP. NET custom control component development chapter 1 Article 3
ASP. NET custom control component development chapter 2 inherit the custom control of webcontrol
ASP. NET custom control component development chapter 3 add events to controls
ASP. NET custom control component development chapter 3 after adding events to controls
ASP. NET custom control component development chapter 4 compositecontrol
ASP. NET custom control component development chapter 4 compositecontrol for compositecontrol
ASP. NET custom control component development chapter 5 template control development
ASP. net2.0 custom control component development chapter 6 in-depth introduction to control attributes
ASP. net2.0 widget Development Video initial experience
ASP. NET development is event-driven. Now let's add events to the control. Before talking about the event, I hope you will be familiar with the C # syntax and delegate
Very event-sensitive.
In fact, the procedure for defining an event is simple:
1. Declare a delegate.
2. Define a class that carries event information.
3. Define events
4. Define a method to notify other objects after a pass event occurs
First, let's clarify our ideas:
1. Select a value from the drop-down list and enter a value in the input box.
2. Click the submit button on the page to trigger a Custom Event validate (verify that the input information is correct ).
We need to transmit the control information to the server when submitting the request. Therefore, we need to define an event information class to send information classes when an event occurs.
To the server.
The event is defined as follows:
1. Define a class carrying event information.
Code
1 Using System;
2 Using System. Collections. Generic;
3 Using System. text;
4
5 Namespace Creditcardform
6 {
7 Public Class Validatecreditcardformeventargs: eventargs
8 {
9 Private String Paymentmethod;
10 Public String Paymentmethod
11 {
12 Get
13 {
14 Return This . Paymentmethod;
15 }
16 }
17
18
19 Private String Creditcardno;
20 Public String Creditcardno
21 {
22 Get
23 {
24 Return This . Creditcardno;
25 }
26 }
27
28
29 Private String CardHolderName;
30 Public String CardHolderName
31 {
32 Get
33 {
34 Return This . CardHolderName;
35 }
36 }
37
38 Private Datetime expirationdate;
39 Public Datetime expirationdate
40 {
41 Get
42 {
43 Return This . Expirationdate;
44 }
45 }
46
47
48 Public Validatecreditcardformeventargs ( String Paymentmenthod, String Creditcardno,
49 String CardHolderName, datetime expirationdate)
50 {
51 This . Paymentmethod = Paymentmenthod;
52 This . Creditcardno = Creditcardno;
53 This . CardHolderName = CardHolderName;
54 This . Expirationdate = Expirationdate;
55 }
56 }
57 }
58
2. Declare a delegate.
Code
1 Using System;
2 Using System. Collections. Generic;
3 Using System. text;
4
5 Namespace Creditcardform
6 {
7 Public Delegate Void Validatecreditcardformeventhandler ( Object Sender, validatecreditcardformeventargs ARGs );
8 }
9
3. Define events
Code
1 Public EventValidatecreditcardformeventhandler validatecreditcardform;
4. Methods for notifying other objects after an event occurs
Code
1
2 // This method is a protected virtual method.
3 Protected Void Onvalidatecreditcardform (validatecreditcardformeventargs ARGs)
4 {
5 If (Validatecreditcardform ! = Null )
6 Handler ( This , ArgS );
7 }
After these steps, the control event is completed. Try it! If you have any questions, let's talk about it later!
I am writing this article today. I hope you can give feedback and hear your thoughts!