. NET 2.0 event mechanism simplifies...

Source: Internet
Author: User

 

Case:The ASPX page has a control. You want to click the control button to perform some operations to obtain a datatable and bind the datatable to the Data Control on the page.

Control CS file:

1. Define proxy

Public   Delegate   Void Retrievetargetshandler ( Object Sender, retrieveeventargs E );

Public ClassRetrieveeventargs: eventargs
{
PrivateDatatable resulttable;

PublicDatatable resulttable
{
Get{ReturnResulttable ;}
}

PublicRetrieveeventargs (datatable RET)
:Base()
{
This. Resulttable=RET;
}
}

2. Define events

Public   Event Retrievetargetshandler retrievetargets;

3. When you click the control button,

Protected   Void Btnsearchbyconditions_click ( Object Sender, eventargs E)
{
Datatable dt =   New Datatable ();
//
Retrievetargets ( This , New Retrieveeventargs (DT ));
}

In page_load of the aspx page,Instantiate the proxy and specify the processing method.

This . Targetsearchbyconditions1.retrievetargets + =   New Retrievetargetshandler (targetsearchbyconditions1_retrievetargets );

VoidTargetsearchbyconditions1_retrievetargets (ObjectSender, retrieveeventargs E)
{
Datatable dt=E. resulttable;
Response. Write ("Yes. Good!");
}

In. net1.1,

If you want your class to triggerEventnameEvent, you need the following elements.

    • the class that holds event data, named eventname eventargs. This class must be exported from system. eventargs.
    • event Delegate, named eventname eventhandler.
    • the class that triggers the event. This class must provide:
      1. event declaration.

          [C #]  public event  eventname  eventhandler  eventname ;  [Visual Basic]  public event eventname as eventnameeventhandler 
      2. Method for triggering the event, named on eventname .

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.