Event bubble control example

Source: Internet
Author: User
. NET Framework developer Guide  
Event bubbling

The ASP. NET page framework provides a technology called "event bubbling" that allows child controls to spread events along their inclusive hierarchy. Event bubbling allows you to easily trigger events in the control hierarchy and process events.ProgramAttaches it to the original control and the control that exposes the bubbling event.

The data binding controls (repeater, datalist, and DataGrid) Use event bubbling to expose command events caused by child controls (within the Project template) as top-level events. Although the ASP. NET Server Control in the. NET Framework uses event bubbling for command events (the event data class isCommandeventargs, But any event defined on the server control can be bubbling.

You can useSystem. Web. UI. ControlThe inherited two methods participate in event bubbling. The two methods are:OnbubbleeventAndRaisebubbleevent. BelowCodeThe snippet shows the signature of these methods.

[C #]Protected virtual bool onbubbleevent (Object source, eventargs ARGs); protected void raisebubbleevent (Object source, eventargs ARGs);[Visual Basic]Overridable protected function onbubbleevent (_ byval source as object, _ byval ARGs as eventargs _) as booleanprotected sub raisebubbleevent (_ byval source as object, _ byval ARGs as eventargs _)

RaisebubbleeventIs IMPLEMENTEDControlAnd cannot be overwritten.RaisebubbleeventSend event data to the parent level of the control along the hierarchy. To process or trigger a bubble event, the control must be overwritten.OnbubbleeventMethod.

Controls that make events bubble execute one of the following three operations.

  • The widget does not perform any operations. At this time, the event automatically bubbles up to its parent level.
  • Control to perform some processing and continue to make the event bubble. To achieve this, the control must be rewritten. Onbubbleevent , And from Onbubbleevent Call Raisebubbleevent . The following code snippet (from the templated data binding control example) bubbles the event after checking the type of event parameters.
      [C #]  protected override bool onbubbleevent (Object source, eventargs E) {If (E is commandeventargs) {// adds information about an item to the // commandevent. templatedlistcommandeventargs ARGs = new templatedlistcommandeventargs (this, source, (commandeventargs) e); raisebubbleevent (this, argS); Return true;} return false ;}  [Visual Basic]  protected overrides function onbubbleevent (source as object, e as eventargs) as Boolean if typeof e is commandeventargs then 'adds information about an item to the 'commandevent. dim ARGs as new templatedlistcommandeventargs (Me, source, ctype (E, commandeventargs) raisebubbleevent (Me, argS) return true end if return falseend function 
  • Controls stop event bubbling and raise and/or process the event. To trigger an event, you must call the method to schedule the event to the listener. To trigger a bubble event, the control must be rewritten.OnbubbleeventTo callOnEventnameMethod. The control that triggers a bubble event usually exposes the bubble event as a top-level event. The following code snippet (from the templated data binding control example) triggers a bubble event.
    [C #]Protected override bool onbubbleevent (Object source, eventargs e) {bool handled = false; If (E is unknown) {templatedlistcommandeventargs Ce = (templatedlistcommandeventargs) E; onitemcommand (CE ); handled = true;} return handled ;}[Visual Basic]Protected overrides function onbubbleevent (source as object, e as eventargs) as Boolean dim handled as Boolean = false if typeof e is too then dim Ce as templatedlistcommandeventargs = ctype (E, identifier) onitemcommand (CE) handled = true end if return handledend Function

For an example of event bubbling, see event bubbling control example and templated data binding control example.

Note:Although the event bubbling method is enabledOnbubbleeventIt complies with the standard. NET Framework naming mode for the method used to trigger the event, but there is no event named bubbleevent. In the control that stops event bubbling, publish the event as a top-level event. For example,DatalistControlCommandThe event is publishedItemcommandEvent. In the. NET FrameworkOnEventnameThe standard signature of a method has a parameter (Protected void oneventname (eventargs E)). However,OnbubbleeventThere are two parameters, because the event originated outside the control; the second parameter provides the source.

So far, this topic explains how the control responds to a bubble event. The following section shows how to create a control that defines a bubble event.

Define a bubble event

If you want the control to enable event bubbling for the event defined by it, the control mustOnEventnameMethod callRaisebubbleevent. You do not need to do any additional work in this control. The following code snippet shows a control that definesCommandEvent.

[C #]Protected virtual void oncommand (commandeventargs e) {commandeventhandler handler = (commandeventhandler) events [eventcommand]; If (handler! = NULL) handler (this, e); // The command event is bubbled up the control hierarchy.Raisebubbleevent (this, e );}[Visual Basic]Protected overridable sub oncommand (E as commandeventargs) dim handler as commandeventhandler = ctype (events (eventcommand), commandeventhandler) if not (handler is nothing) Then handler (Me, E) end if 'the command event is bubbled up the control hierarchy. raisebubbleevent (Me, e) End sub

Note:Event bubbling is not limited to command events. You can use the mechanism described here to bubble up any event.

See

Event bubble control example | templated data binding control example

Event bubble control example

The following Custom ControlsEventbubblerIt illustrates a simple event bubble situation.EventbubblerIs a composite control that contains Textbox, button, and label controls.EventbubblerBubble command events from buttons to parent container controls (itself) and expose them as top-level events. To generate this example, see the instructions in the server control example.

For more practical examples, see examples of templated data binding controls.

 [C #] Using system; using system. web; using system. web. ui; using system. web. UI. webcontrols; namespace customcontrols {public class eventbubbler: control, inamingcontainer {private int number = 100; private label Label; private textbox box1; private textbox box2; public event eventhandler click; public event eventhandler reset; public event eventhandler submit; Public String label {get {ensurechildcontro Ls (); return label. text;} set {ensurechildcontrols (); label. TEXT = value ;}} public int number {get {return number ;}set {number = value ;}} Public String text1 {get {ensurechildcontrols (); Return box1.text ;} set {ensurechildcontrols (); box1.text = value ;}} Public String text2 {get {ensurechildcontrols (); Return box2.text;} set {ensurechildcontrols (); box2.text = value ;}} prote CTED override void createchildcontrols () {controls. add (New literalcontrol (" [Visual Basic] Option explicitoption strictimports systemimports system. webimports system. web. uiimports system. web. UI. webcontrolsnamespace customcontrols public class eventbubbler inherits control implements inamingcontainer private _ number as integer = 100 private _ label as label private _ box1 as textbox private _ box2 as textbox public event click as eventhandler public event reset eventhandler public event submit as eventhandler public property label () as string get ensurechildcontrols () return _ label. text end get set ensurechildcontrols () _ label. TEXT = value end set end property public property number () as integer get return _ number end get set _ number = value end set end property public property text1 () as string get ensurechildcontrols () return _ box1.text end get set ensurechildcontrols () _ box1.text = value end set end property public property text2 () as string get ensurechildcontrols () return _ box2.text end get set ensurechildcontrols () _ box2.text = value end set end property protected overrides sub createchildcontrols () controls. add (New literalcontrol ("Use the event bubble control on the page

The following ASP. NET page uses the Custom Event bubble ControlEventbubblerAnd attach the event handler to its top-level events.

 [C #] <% @ Register tagprefix = "Custom" namespace = "customcontrols" assembly = "customcontrols" %> <HTML> <script language = "C #" runat = Server> private void clickhandler (object sender, eventargs e) {mycontrol. label = "you clicked the <B> click </B> button";} private void resethandler (Object sender, eventargs e) {mycontrol. text1 = "0"; mycontrol. text2 = "0";} private void submithandler (Object sender, eventargs E) {If (int32.parse (mycontrol. text1) + int32.parse (mycontrol. text2) = mycontrol. number) mycontrol. label = "<H2> you won a million dollars !!!! </H2> "; else mycontrol. label = "sorry, try again. the numbers you entered don't add up to "+" The Hidden number. ";}</SCRIPT> <body>  [Visual Basic] <% @ Register tagprefix = "Custom" namespace = "customcontrols" assembly = "customcontrols" %> <HTML> <script language = "VB" runat = Server> private sub clickhandler (sender as object, e As eventargs) mycontrol. label = "you clicked the <B> click </B> button" End sub private sub resethandler (sender as object, e as eventargs) mycontrol. text1 = "0" mycontrol. text2 = "0" End sub private sub submithandler (sender S object, e as eventargs) If int32.parse (mycontrol. text1) + int32.parse (mycontrol. text2) = mycontrol. number then mycontrol. label = "<H2> you won a million dollars !!!! </H2> "else mycontrol. label = "sorry, try again. the numbers you entered don't add up to "&" The Hidden number. "End if end sub </SCRIPT> <body> 

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.