Events of C # and ASP.

Source: Internet
Author: User

An event is a programmatic way to pass messages between classes and classes, or to trigger new behavior. By providing a handle to the event, you can associate the control with the executable code,

Executes the corresponding event-handling code when the user clicks the button control to trigger the Click event.

The declaration of an event is implemented by a delegate. Define the delegate, then define the event with the delegate, and the process that triggers the event is essentially the calling delegate. The event declaration syntax format is as follows:

public delegate void EventHandler (Object Sender,eventargs e);//define Delegate

Public event EventHandler myevent;//defining events

The EventHandler delegate defines two parameters, belonging to the object type and the EventArgs type, respectively. If more parameters are required, they can be implemented by deriving the EventArgs class.

Sender represents the object that triggered the event, and E is used to pass parameters in the event. For example, if the user clicks the button, sender indicates the button and E indicates the click Time parameter.

The MyEvent event uses the EventHandler delegate definition, which uses the public modifier, or it can use modifiers such as private,protected.

Example: Increase the account amount in the Accountevent class and apply the event

Definition of the Accountevent class:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 6 7  Public classaccountevent8 {9     Private string_id;Ten     Private string_name; One     Private decimal_balance; A     //Defining Overdraw Events -      Public EventEventHandler overdraw; -      Public voidOnoverdraw (Objectsender, EventArgs e) the     { -         if(Overdraw! =NULL) -         { -Overdraw ( This, e); +  -         } +     } A     //Deposit Method at      Public voidDeposit (decimalamount) -     { -         //first check whether the amount of deposit is greater than 0 -         if(Amount >0) -         { -_balance + =amount; in         } -         Else to         { +             Throw NewException ("The amount of the deposit cannot be less than or equal to 0!! "); -         } the     } *     //Withdrawal Method $     //and add the code that triggers the event in this methodPanax Notoginseng      Public voidAcquire (decimalamount) -     { the         if(Amount <_balance) +         { A_balance-=amount; the         } +         Else -         { $Onoverdraw ( This, eventargs.empty); $             return; -         } -     } the     -      Public stringIDWuyi     { the         Get{return_id;} -         Set{_id =value;} Wu     } -      Public stringName About     { $         Get{return_name;} -         Set{_name =value;} -     } -      Public decimalBalance A     { +         Get{return_balance;} the         Set{_balance =value;} -     } $     //Construction Method the      PublicAccountevent (stringIdstringNamedecimalbalance) the     { the_id =ID; the_name =name; -_balance =balance; in  the     } the     About      the}

At this point, the event has been declared and the event trigger points have been added. However, to use events on an ASP. NET page, you also need to register the event with the operator "+ =" and write the event-handling code.

Here's how to use the Overdraw event.

AccountEventPage.aspx.cs's Code:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSystem.Web.UI;6 usingSystem.Web.UI.WebControls;7 8  Public Partial classChap3_AccountEventPage:System.Web.UI.Page9 {Ten     protected voidPage_Load (Objectsender, EventArgs e) One     { AAccountevent accountevent =NewAccountevent ("03012","Li Ming", $);//Establish Accountevent object, account initial amount is -         //Registering Overdraw Events -Accountevent.overdraw + =NewEventHandler (account_overdraw); theAccountevent.acquire ( -);//Withdrawals -  -     } -     //overdraw Event handling code +     Private voidAccount_overdraw (Objectsender, EventArgs e) -     { +Response.Write ("the account amount is insufficient!!! "); A     } at}

Browse the Accounteventpage.aspx rendering interface as follows:

Program Description:

When the program executes the "account." Acquire (400); " , because the amount of withdrawals to the account is greater than the account amount, the else portion of the IF statement in the acquire method is executed, the event overdraw is triggered, and the Account_overdraw () method is executed, and the output information "account amount is insufficient!!!" ”。

In this example, the steps used for the event are:

1. Define the event.

2. Set the trigger point for the event.

3. Register the event with the operator "+ =".

4. Write the event-handling code.

1, 22 steps are done in the definition of the class, and 3, 4 steps are done in the ASPX file.

Events of C # and ASP.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.