Two programming methods of event in Asp.net Component Programming

Source: Internet
Author: User

Below isComponentsCode:
Using system;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. componentmodel;

Namespace nseventstudy
{
Public Delegate void twoeventhandle (INT flag );

Public class eventstudy: system. Web. UI. webcontrols. webcontrol
{

/// // The first method for defining an event ////////////////////

Public event twoeventhandle twoevent;

Public void execute (INT flag)
{
Twoevent (FLAG );
}

//// // The second method for defining the event ////////////////// //

Private Static object _ process = new object ();
Public event twoeventhandle threeevent
{
Add
{
Events. addhandler (_ process, value );
}
Remove
{
Events. removehandler (_ process, value );
}
}

Public void innerexecute (INT flag)
{
Twoeventhandle handle = (twoeventhandle) events [_ process];
If (handle! = NULL)
{
Handle (FLAG );
}
Else
{
This. raisebubbleevent (this, null );
}
}

Protected override void render (htmltextwriter writer)
{
Base. Render (writer );
Writer. writeline ("I love you, China ");
}

}
}

TestProgram:
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;

Namespace testevent
{
/// <Summary>
/// Summary of webform1.
/// </Summary>
Public class webform1: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. Button button1;
Protected nseventstudy. eventstudy eventstudy1;

Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
}

# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. eventstudy1.threeevent + = new nseventstudy. twoeventhandle (this. eventstudy1_threeevent );
This. eventstudy1.twoevent + = new nseventstudy. twoeventhandle (this. eventstudy1_twoevent );
This. button1.click + = new system. eventhandler (this. button#click );
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion

Private void eventstudy1_twoevent (INT flag)
{
This. response. Write ("<SCRIPT> javascript: Alert ('twoevent event trigger ') </SCRIPT> ");
}

Private void eventstudy1_threeevent (INT flag)
{
This. response. Write ("<SCRIPT> javascript: Alert ('threeevent trigger ') </SCRIPT> ");
}

Private void button#click (Object sender, system. eventargs E)
{
This. eventstudy1.execute (6 );
This. eventstudy1.innerexecute (10 );
}
}
}

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.