Event bubble control example

Source: Internet
Author: User
ASP. net, many user controls are often used, and sometimes several steps are put together (that is, next). At this time, the event execution order is complex and troublesome. The problem I encountered was that I had to execute a piece of code in pageload in Step 4. In this step, the Code does not need to be executed when the page is returned. I can't use ispostback. The page is just loaded and will not be executed in Step 4. I hope this article will be useful.
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 attach event handlers to the original controls and controls that expose events.
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 is an event derived from commandeventargs), any event defined on the server control can be bubbling.
Controls can be involved in event bubbling through two methods inherited from the base class system. Web. UI. Control. The two methods are onbubbleevent and raisebubbleevent. The following code snippet shows the signatures of these methods.

Code:
[C #] <br/> protected virtual bool onbubbleevent (<br/> Object source, <br/> eventargs ARGs <br/> ); <br/> protected void raisebubbleevent (<br/> Object source, <br/> eventargs ARGs <br/> ); <br/> [Visual Basic] <br/> overridable protected function onbubbleevent (_ <br/> byval source as object, _ <br/> byval ARGs as eventargs _ <br/>) as Boolean <br/> protected sub raisebubbleevent (_ <br/> byval source as object, _ <br/> byval ARGs as eventargs _ <br/>) <br/>
[Ctrl + A select all]

The implementation of raisebubbleevent is provided by control and cannot be overwritten. Raisebubbleevent sends event data to the parent level of the control along the hierarchy. To process or trigger a bubble event, the control must override the onbubbleevent method.
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 override onbubbleevent and call raisebubbleevent from onbubbleevent. The following code snippet (from the templated data binding control example) bubbles the event after checking the type of event parameters.

Code:
<Br/> [C #] <br/> protected override bool onbubbleevent (Object source, eventargs e) {<br/> If (E is commandeventargs) {<br/> // adds information about an item to the <br/> // commandevent. <br/> templatedlistcommandeventargs ARGs = <br/> New templatedlistcommandeventargs (this, source, (commandeventargs) e); <br/> raisebubbleevent (this, argS ); <br/> return true; <br/>}< br/> return false; <br/>}< br/> [Visual Basic] <br/> protected overrides function onbubbleevent (source as object, e as eventargs) as Boolean <br/> If typeof e is commandeventargs then <br/> 'adds information about an item to the <br/> 'commandevent. <br/> dim ARGs as new templatedlistcommandeventargs (Me, source, ctype (E, commandeventargs) <br/> raisebubbleevent (Me, argS) <br/> return true <br/> end if <br/> return false <br/> end function <br/>
[Ctrl + A select all]

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 override the onbubbleevent to call the oneventname method that triggers the event. 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.

Code:
<Br/> [C #] <br/> protected override bool onbubbleevent (Object source, eventargs e) {<br/> bool handled = false; <br/> If (E is templatedlistcommandeventargs) {<br/> templatedlistcommandeventargs Ce = (templatedlistcommandeventargs) E; <br/> onitemcommand (CE); <br/> handled = true; <br/>}< br/> return handled; <br/>}< br/> [Visual Basic] <br/> protected overrides function onbubbleevent (source as object, e As eventargs) as Boolean <br/> dim handled as Boolean = false <br/> If typeof e is templatedlistcommandeventargs then <br/> dim Ce as templatedlistcommandeventargs = ctype (E, templatedlistcommandeventargs) <br/> onitemcommand (CE) <br/> handled = true <br/> end if <br/> return handled <br/> end function <br/>
[Ctrl + A select all]

For an example of event bubbling, see event bubbling control example and templated data binding control example.
  Note:Although the onbubbleevent method that enables event bubbling complies with the standard. NET Framework naming mode for the method used to raise the event, there is no event named bubbleevent. In the control that stops event bubbling, publish the event as a top-level event. For example, the datalist control discloses the command event of the control in its template as an itemcommand event. In the. NET Framework, the standard signature of the oneventname method has a parameter (protected void oneventname (eventargs E )). However, onbubbleevent has 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
To enable event bubbling for the event defined by the control, the control must call raisebubbleevent from the oneventname method that triggers the event. You do not need to do any additional work in this control. The following code snippet shows a control that defines a command event that enables bubbling.

Code:
[C #] <br/> protected virtual void oncommand (commandeventargs e) {<br/> commandeventhandler handler = (commandeventhandler) events [eventcommand]; <br/> If (handler! = NULL) <br/> handler (this, e); <br/> // The command event is bubbled up the control hierarchy. <br/> raisebubbleevent (this, e); <br/>}< br/> [Visual Basic] <br/> protected overridable sub oncommand (E as commandeventargs) <br/> dim handler as commandeventhandler = ctype (events (eventcommand), commandeventhandler) <br/> if not (handler is nothing) Then <br/> handler (Me, E) <br/> end if <br/> 'the command event is bubbled up the control hierarchy. <br/> raisebubbleevent (Me, e) <br/> end sub <br/>
[Ctrl + A select all]

  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 control eventbubbler illustrates a simple event bubble situation. Eventbubbler is a composite control that contains Textbox, button, and label controls. Eventbubbler bubbles command events from buttons to parent container controls (itself) and exposes 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.

Code:
[C #] <br/> using system; <br/> using system. web; <br/> using system. web. ui; <br/> using system. web. UI. webcontrols; <br/> namespace customcontrols <br/>{< br/> <br/> public class eventbubbler: control, inamingcontainer <br/> {<br/> private int number = 100; <br/> private label Label; <br/> private textbox box1; <br/> private textbox box2; <br/> public event eventhandler click; <br/> Public even T eventhandler reset; <br/> public event eventhandler submit; <br/> Public String label <br/> {<br/> Get <br/> {<br/> ensurechildcontrols (); <br/> return label. text; <br/>}< br/> set <br/>{< br/> ensurechildcontrols (); <br/> label. TEXT = value; <br/>}< br/> <br/> Public int number <br/>{< br/> Get <br/>{< br/> return number; <br/>}< br/> set <br/> {<br/> Number = value; <br />}< Br/> <br/> Public String text1 <br/>{< br/> Get <br/>{< br/> ensurechildcontrols (); <br/> return box1.text; <br/>}< br/> set <br/>{< br/> ensurechildcontrols (); <br/> box1.text = value; <br/>}< br/> <br/> Public String text2 <br/>{< br/> Get <br/>{< br/> ensurechildcontrols (); <br/> return box2.text; <br/>}< br/> set <br/>{< br/> ensurechildcontrols (); <br/> box2. TEXT = value; <br/>}< br/> <br/> protected override void createchildcontrols () <br/>{< br/> <br/> controls. add (New literalcontrol ("<p> enter a number:"); <br/> box1 = new Textbox (); <br/> box1.text = "0"; <br/> controls. add (box1); <br/> controls. add (New literalcontrol ("</p>"); <br/> controls. add (New literalcontrol ("<p> enter another number:"); <br/> <Br/> box2 = new Textbox (); <br/> box2.text = "0"; <br/> controls. add (box2); <br/> controls. add (New literalcontrol ("</p>"); <br/> button button1 = new button (); <br/> button1.text = "click "; <br/> button1.commandname = "click"; <br/> controls. add (button1); <br/> button button2 = new button (); <br/> button2.text = "reset "; <br/> button2.commandname = "reset"; <br/> controls. Add (button2); <br/> button button3 = new button (); <br/> button3.text = "Submit "; <br/> button3.commandname = "Submit"; <br/> controls. add (button3); <br/> controls. add (New literalcontrol ("<br>"); <br/> label = new label (); <br/> label. height = 50; <br/> label. width = 500; <br/> label. TEXT = "click a button. "; <br/> controls. add (Label); <br/> <br/>}< br/> <br/> protected overr IDE bool onbubbleevent (Object source, eventargs e) <br/>{< br/> bool handled = false; <br/> If (E is commandeventargs) <br/>{< br/> commandeventargs Ce = (commandeventargs) E; <br/> If (CE. commandname = "click") <br/>{< br/> onclick (CE); <br/> handled = true; <br/>}< br/> else if (CE. commandname = "reset") <br/>{< br/> onreset (CE); <br/> handled = true; <br/>}< br/> else if (CE. commandna Me = "Submit") <br/>{< br/> onsubmit (CE); <br/> handled = true; <br/>}< br/> return handled; <br/>}< br/> <br/> protected virtual void onclick (eventargs e) <br/>{< br/> If (Click! = NULL) <br/>{< br/> click (this, e ); <br/>}< br/> <br/> protected virtual void onreset (eventargs e) <br/>{< br/> If (reset! = NULL) <br/>{< br/> Reset (this, e ); <br/>}< br/> <br/> protected virtual void onsubmit (eventargs e) <br/>{< br/> If (submit! = NULL) <br/>{< br/> submit (this, e ); <br/>}< br/> [Visual Basic] <br/> Option explicit <br/> Option strict <br/> imports system. web <br/> imports system. web. ui <br/> imports system. web. UI. webcontrols <br/> namespace customcontrols <br/> public class eventbubbler <br/> inherits Control <br/> implements inamingcontainer <br/> private _ number as integer = 100 <br/> private _ label as label <br/> private _ box1 as textbox <br/> private _ box2 as textbox <br/> public event click as eventhandler <br/> public event reset as eventhandler <br/> public event submit as eventhandler <br/> Public Property label () as string <br/> Get <br/> ensurechildcontrols () <br/> return _ label. text <br/> end get <br/> set <br/> ensurechildcontrols () <br/> _ label. TEXT = value <br/> end set <br/> end property <br/> Public Property number () as integer <br/> Get <br/> return _ number <br/> end get <br/> set <br/> _ number = value <br/> end set <br/> end property <br/> Public Property text1 () as string <br/> Get <br/> ensurechildcontrols () <br/> return _ box1.text <br/> end get <br/> set <br/> ensurechildcontrols () <br/> _ box1.text = value <br/> end set <br/> end property <br/> Public Property text2 () as string <br/> Get <br/> ensurechildcontrols () <br/> return _ box2.text <br/> end get <br/> set <br/> ensurechildcontrols () <br/> _ box2.text = value <br/> end set <br/> end property <br/> protected overrides sub createchildcontrols () <br/> controls. add (New literalcontrol ("<p> enter a number:") <br/> _ box1 = new Textbox () <br/> _ box1.text = "0" <br/> controls. add (_ box1) <br/> controls. add (New literalcontrol ("</p>") <br/> controls. add (New literalcontrol ("<p> enter another number:") <br/> _ box2 = new Textbox () <br/> _ box2.text = "0" <br/> controls. add (_ box2) <br/> controls. add (New literalcontrol ("</p>") <br/> dim button1 as new button () <br/> button1.text = "click" <br/> button1.commandname = "click" <br/> controls. add (button1) <br/> dim button2 as new button () <br/> button2.text = "reset" <br/> button2.commandname = "reset" <br/> controls. add (button2) <br/> dim button3 as new button () <br/> button3.text = "Submit" <br/> button3.commandname = "Submit" <br/> controls. add (button3) <br/> controls. add (New literalcontrol ("<br>") <br/> _ label = new label () <br/> _ label. height = unit. pixel (50) <br/> _ label. width = unit. pixel (500) <br/> _ label. TEXT = "click a button. "<br/> controls. add (_ label) <br/> end sub <br/> protected overrides function onbubbleevent (source as object, e as eventargs) as Boolean <br/> dim handled as Boolean = false <br/> If typeof e is commandeventargs then <br/> dim Ce as commandeventargs = ctype (E, commandeventargs) <br/> if CE. commandname = "click" then <br/> onclick (CE) <br/> handled = true <br/> else <br/> if CE. commandname = "reset" then <br/> onreset (CE) <br/> handled = true <br/> else <br/> if CE. commandname = "Submit" then <br/> onsubmit (CE) <br/> handled = true <br/> end if <br/> return handled <br/> end function <br/> protected overridable sub onclick (E as eventargs) <br/> raiseevent click (Me, e) <br/> end sub <br/> protected overridable sub onreset (E as eventargs) <br/> raiseevent reset (Me, e) <br/> end sub <br/> protected overridable sub onsubmit (E as eventargs) <br/> raiseevent submit (Me, e) <br/> end sub <br/> end class <br/> end namespace <br/>
[Ctrl + A select all]

  Use the event bubble control on the page
The following ASP. NET page uses the Custom Event bubble control eventbubbler and attaches the event handler to its top-level event.

Code:
[C #] <br/> <% @ register tagprefix = "Custom" namespace = "customcontrols" assembly = "customcontrols" %> <br/> <HTML> <br/> <script language = "C #" runat = Server> <br/> private void clickhandler (Object sender, eventargs e) <br/>{< br/> mycontrol. label = "you clicked the <B> click </B> button"; <br/>}< br/> private void resethandler (Object sender, eventargs E) <br/>{< br/> mycontrol. text1 = "0"; <br/> mycont ROL. text2 = "0"; <br/>}< br/> private void submithandler (Object sender, eventargs e) <br/>{< br/> If (int32.parse (mycontrol. text1) + int32.parse (mycontrol. text2) = mycontrol. number) <br/> mycontrol. label = "<H2> you won a million dollars !!!! </H2> "; <br/> else <br/> mycontrol. label = "sorry, try again. the numbers you entered don't add up to "+ <br/>" The Hidden number. "; <br/>}< br/> </SCRIPT> <br/> <body> <br/> <p> the mystery sum game </p> <br/> <form runat = Server> <br/> <custom: eventbubbler id = "mycontrol" onclick = "clickhandler" <br/> onreset = "resethandler" onsubmit = "submithandler" number = "10" Runa T = server/> <br/> </form> <br/> </body> <br/> </ptml> <br/> [Visual Basic] <br/> <% @ register tagprefix = "Custom" namespace = "customcontrols" assembly = "customcontrols" %> <br/> <HTML> <br/> <script language = "VB" runat = Server> <br/> private sub clickhandler (sender as object, e As eventargs) <br/> mycontrol. label = "you clicked the <B> click </B> button" <br/> end sub <br/> private sub resethan Dler (sender as object, e as eventargs) <br/> mycontrol. text1 = "0" <br/> mycontrol. text2 = "0" <br/> end sub <br/> private sub submithandler (sender as object, e as eventargs) <br/> If int32.parse (mycontrol. text1) + int32.parse (mycontrol. text2) = mycontrol. number then <br/> mycontrol. label = "<H2> you won a million dollars !!!! </H2> "<br/> else <br/> mycontrol. label = "sorry, try again. the numbers you entered don't add up to "&" The Hidden number. "<br/> end if <br/> end sub <br/> </SCRIPT> <br/> <body> <br/> <p> the mystery sum game </p> <br/> <form runat = Server> <br/> <custom: eventbubbler id = "mycontrol" onclick = "clickhandler" <br/> onreset = "resethandler" onsubmit = "submithandler" number = "10" runat = server/> <br/> </form> <br/> </body> <br/> </ptml> <br/>
[Ctrl + A select all]

 

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.