liveBook · Manning

Event propagation

As explained in Pro ASP.NET Core 7, Tenth Edition

Event propagation is the browser mechanism by which an event is dispatched through related elements in an interface hierarchy.

A browser event passes up the chain of ancestor elements. In a Blazor component, clicking a button inside a div can therefore invoke both the button’s @onclick handler and the div’s @onclick handler. section 33.4.1

Two counters from one click

Put an @onclick handler on a button that increments one counter and another on its enclosing div that increments a second counter. Clicking the button updates both. Add @onclick:stopPropagation to the button to prevent the event from reaching the ancestor handler. section 33.4.1

Do not confuse propagation with default behavior. @onclick:preventDefault addresses a different problem: a button inside a form submits the form when clicked, even if it has an @onclick handler. section 33.4.1

Table 33.5 The event configuration parameters (view table figure)

Name

Description

@on{event}:preventDefault

This parameter determines whether the default event for an element is triggered.

@on{event}:stopPropagation

This parameter determines whether an event is propagated to its ancestor elements.

Compare the two Blazor event parameters: stopPropagation controls propagation to ancestors; preventDefault controls the element’s default event behavior. section 33.4.1

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains propagation through ancestor elements and shows the parameter that stops it, without detailing the browser event lifecycle.
Examples
Uses a button inside a clickable div to show one click updating two counters.
What you can do after
Operate/configure it with @onclick:stopPropagation to prevent an ancestor’s click handler from running.
How it connects
Relates event propagation to Blazor event handlers, ancestor elements, and default event behavior.

Also explained in

Compare how 4 books explain event propagation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.