--- title: ASP.NET Core Blazor event handling author: guardrex description: Learn about Blazor's event handling features, including event argument types, event callbacks, and managing default browser events. monikerRange: '>= aspnetcore-3.1' ms.author: riande ms.custom: mvc ms.date: 11/12/2024 uid: blazor/components/event-handling --- # ASP.NET Core Blazor event handling [!INCLUDE[](~/includes/not-latest-version.md)] This article explains Blazor's event handling features, including event argument types, event callbacks, and managing default browser events. ## Delegate event handlers Specify delegate event handlers in Razor component markup with [`@on{DOM EVENT}="{DELEGATE}"`](xref:mvc/views/razor#onevent) Razor syntax: * The `{DOM EVENT}` placeholder is a [DOM event](https://developer.mozilla.org/docs/Web/Events) (for example, `click`). * The `{DELEGATE}` placeholder is the C# delegate event handler. For event handling: :::moniker range=">= aspnetcore-8.0" * Delegate event handlers in Blazor Web Apps are only called in components that adopt an interactive render mode. The examples throughout this article assume that the app adopts an interactive render mode globally in the app's root component, typically the `App` component. For more information, see . * Asynchronous delegate event handlers that return a are supported. * Delegate event handlers automatically trigger a UI render, so there's no need to manually call [`StateHasChanged`](xref:blazor/components/lifecycle#state-changes-statehaschanged). * Exceptions are logged. :::moniker-end :::moniker range="< aspnetcore-8.0" * Asynchronous delegate event handlers that return a are supported. * Delegate event handlers automatically trigger a UI render, so there's no need to manually call [`StateHasChanged`](xref:blazor/components/lifecycle#state-changes-statehaschanged). * Exceptions are logged. :::moniker-end The following code: * Calls the `UpdateHeading` method when the button is selected in the UI. * Calls the `CheckChanged` method when the checkbox is changed in the UI. :::moniker range=">= aspnetcore-9.0" `EventHandler1.razor`: :::code language="razor" source="~/../blazor-samples/9.0/BlazorSample_BlazorWebApp/Components/Pages/EventHandler1.razor"::: :::moniker-end :::moniker range=">= aspnetcore-8.0 < aspnetcore-9.0" `EventHandler1.razor`: :::code language="razor" source="~/../blazor-samples/8.0/BlazorSample_BlazorWebApp/Components/Pages/EventHandler1.razor"::: :::moniker-end :::moniker range=">= aspnetcore-7.0 < aspnetcore-8.0" `EventHandlerExample1.razor`: :::code language="razor" source="~/../blazor-samples/7.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample1.razor"::: :::moniker-end :::moniker range=">= aspnetcore-6.0 < aspnetcore-7.0" `EventHandlerExample1.razor`: :::code language="razor" source="~/../blazor-samples/6.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample1.razor"::: :::moniker-end :::moniker range=">= aspnetcore-5.0 < aspnetcore-6.0" `EventHandlerExample1.razor`: :::code language="razor" source="~/../blazor-samples/5.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample1.razor"::: :::moniker-end :::moniker range="< aspnetcore-5.0" `EventHandlerExample1.razor`: :::code language="razor" source="~/../blazor-samples/3.1/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample1.razor"::: :::moniker-end In the following example, `UpdateHeading`: * Is called asynchronously when the button is selected. * Waits two seconds before updating the heading. :::moniker range=">= aspnetcore-9.0" `EventHandler2.razor`: :::code language="razor" source="~/../blazor-samples/9.0/BlazorSample_BlazorWebApp/Components/Pages/EventHandler2.razor"::: :::moniker-end :::moniker range=">= aspnetcore-8.0 < aspnetcore-9.0" `EventHandler2.razor`: :::code language="razor" source="~/../blazor-samples/8.0/BlazorSample_BlazorWebApp/Components/Pages/EventHandler2.razor"::: :::moniker-end :::moniker range=">= aspnetcore-7.0 < aspnetcore-8.0" `EventHandlerExample2.razor`: :::code language="razor" source="~/../blazor-samples/7.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample2.razor"::: :::moniker-end :::moniker range=">= aspnetcore-6.0 < aspnetcore-7.0" `EventHandlerExample2.razor`: :::code language="razor" source="~/../blazor-samples/6.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample2.razor"::: :::moniker-end :::moniker range=">= aspnetcore-5.0 < aspnetcore-6.0" `EventHandlerExample2.razor`: :::code language="razor" source="~/../blazor-samples/5.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample2.razor"::: :::moniker-end :::moniker range="< aspnetcore-5.0" `EventHandlerExample2.razor`: :::code language="razor" source="~/../blazor-samples/3.1/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample2.razor"::: :::moniker-end ## Built-in event arguments For events that support an event argument type, specifying an event parameter in the event method definition is only necessary if the event type is used in the method. In the following example, is used in the `ReportPointerLocation` method to set message text that reports the mouse coordinates when the user selects a button in the UI. :::moniker range=">= aspnetcore-9.0" `EventHandler3.razor`: :::code language="razor" source="~/../blazor-samples/9.0/BlazorSample_BlazorWebApp/Components/Pages/EventHandler3.razor"::: :::moniker-end :::moniker range=">= aspnetcore-8.0 < aspnetcore-9.0" `EventHandler3.razor`: :::code language="razor" source="~/../blazor-samples/8.0/BlazorSample_BlazorWebApp/Components/Pages/EventHandler3.razor"::: :::moniker-end :::moniker range=">= aspnetcore-7.0 < aspnetcore-8.0" `EventHandlerExample3.razor`: :::code language="razor" source="~/../blazor-samples/7.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample3.razor"::: :::moniker-end :::moniker range=">= aspnetcore-6.0 < aspnetcore-7.0" `EventHandlerExample3.razor`: :::code language="razor" source="~/../blazor-samples/6.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample3.razor"::: :::moniker-end :::moniker range=">= aspnetcore-5.0 < aspnetcore-6.0" `EventHandlerExample3.razor`: :::code language="razor" source="~/../blazor-samples/5.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample3.razor"::: :::moniker-end :::moniker range="< aspnetcore-5.0" `EventHandlerExample3.razor`: :::code language="razor" source="~/../blazor-samples/3.1/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample3.razor"::: :::moniker-end Supported are shown in the following table. | Event | Class | DOM notes | | ---------------- | ------ | --- | | Clipboard | | | | Drag | | and hold dragged item data.

Implement drag and drop in Blazor apps using [JS interop](xref:blazor/js-interop/call-javascript-from-dotnet) with [HTML Drag and Drop API](https://developer.mozilla.org/docs/Web/API/HTML_Drag_and_Drop_API). | | Error | | | | Event | | holds attributes to configure the mappings between event names and event argument types. | | Focus | | Doesn't include support for `relatedTarget`. | | Input | | | | Keyboard | | | | Mouse | | | | Mouse pointer | | | | Mouse wheel | | | | Progress | | | | Touch | | represents a single contact point on a touch-sensitive device. | For more information, see the following resources: * [`EventArgs` classes in the ASP.NET Core reference source (dotnet/aspnetcore `main` branch)](https://github.com/dotnet/aspnetcore/tree/main/src/Components/Web/src/Web) [!INCLUDE[](~/includes/aspnetcore-repo-ref-source-links.md)] * holds attributes to configure the mappings between event names and event argument types. :::moniker range=">= aspnetcore-6.0" ## Custom event arguments Blazor supports custom event arguments, which enable you to pass arbitrary data to .NET event handlers with custom events. ### General configuration Custom events with custom event arguments are generally enabled with the following steps. In JavaScript, define a function for building the custom event argument object from the source event: ```javascript function eventArgsCreator(event) { return { customProperty1: 'any value for property 1', customProperty2: event.srcElement.id }; } ``` The `event` parameter is a [DOM Event (MDN documentation)](https://developer.mozilla.org/docs/Web/API/Event). Register the custom event with the preceding handler in a [JavaScript initializer](xref:blazor/fundamentals/startup#javascript-initializers). Provide the appropriate browser event name to `browserEventName`, which for the example shown in this section is `click` for a button selection in the UI. `wwwroot/{PACKAGE ID/ASSEMBLY NAME}.lib.module.js` (the `{PACKAGE ID/ASSEMBLY NAME}` placeholder is the package ID or assembly name of the app): :::moniker-end :::moniker range=">= aspnetcore-8.0" For a Blazor Web App: ```javascript export function afterWebStarted(blazor) { blazor.registerCustomEventType('customevent', { browserEventName: 'click', createEventArgs: eventArgsCreator }); } ``` For a Blazor Server or Blazor WebAssembly app: :::moniker-end :::moniker range=">= aspnetcore-6.0" ```javascript export function afterStarted(blazor) { blazor.registerCustomEventType('customevent', { browserEventName: 'click', createEventArgs: eventArgsCreator }); } ``` The call to `registerCustomEventType` is performed in a script only once per event. For the call to `registerCustomEventType`, use the `blazor` parameter (lowercase `b`) provided by the Blazor start event. Although the registration is valid when using the `Blazor` object (uppercase `B`), the preferred approach is to use the parameter. The custom event name, `customevent` in the preceding example, must not match a reserved Blazor event name. The reserved names can be found in the [Blazor framework reference source (see the calls to the `registerBuiltInEventType` function)](https://github.com/dotnet/aspnetcore/blob/main/src/Components/Web.JS/src/Rendering/Events/EventTypes.ts). [!INCLUDE[](~/includes/aspnetcore-repo-ref-source-links.md)] Define a class for the event arguments: ```csharp namespace BlazorSample.CustomEvents; public class CustomEventArgs : EventArgs { public string? CustomProperty1 {get; set;} public string? CustomProperty2 {get; set;} } ``` Wire up the custom event with the event arguments by adding an [`[EventHandler]` attribute](xref:Microsoft.AspNetCore.Components.EventHandlerAttribute) annotation for the custom event: * In order for the compiler to find the `[EventHandler]` class, it must be placed into a C# class file (`.cs`), making it a normal top-level class. * Mark the class `public`. * The class doesn't require members. * The class *must* be called "`EventHandlers`" in order to be found by the Razor compiler. * Place the class under a namespace specific to your app. * Import the namespace into the Razor component (`.razor`) where the event is used. ```csharp using Microsoft.AspNetCore.Components; namespace BlazorSample.CustomEvents; [EventHandler("oncustomevent", typeof(CustomEventArgs), enableStopPropagation: true, enablePreventDefault: true)] public static class EventHandlers { } ``` Register the event handler on one or more HTML elements. Access the data that was passed in from JavaScript in the delegate handler method: ```razor @using BlazorSample.CustomEvents @if (!string.IsNullOrEmpty(propVal1) && !string.IsNullOrEmpty(propVal2)) {
  • propVal1: @propVal1
  • propVal2: @propVal2
} @code { private string? propVal1; private string? propVal2; private void HandleCustomEvent(CustomEventArgs eventArgs) { propVal1 = eventArgs.CustomProperty1; propVal2 = eventArgs.CustomProperty2; } } ``` If the `@oncustomevent` attribute isn't recognized by [IntelliSense](/visualstudio/ide/using-intellisense), make sure that the component or the `_Imports.razor` file contains an `@using` statement for the namespace containing the `EventHandler` class. Whenever the custom event is fired on the DOM, the event handler is called with the data passed from the JavaScript. If you're attempting to fire a custom event, [`bubbles`](https://developer.mozilla.org/docs/Web/API/Event/bubbles) must be enabled by setting its value to `true`. Otherwise, the event doesn't reach the Blazor handler for processing into the C# custom [`[EventHandler]` attribute](xref:Microsoft.AspNetCore.Components.EventHandlerAttribute) class. For more information, see [MDN Web Docs: Event bubbling](https://developer.mozilla.org/docs/Web/Guide/Events/Creating_and_triggering_events#event_bubbling). ### Custom clipboard paste event example The following example receives a custom clipboard paste event that includes the time of the paste and the user's pasted text. Declare a custom name (`oncustompaste`) for the event and a .NET class (`CustomPasteEventArgs`) to hold the event arguments for this event: `CustomEvents.cs`: ```csharp using Microsoft.AspNetCore.Components; namespace BlazorSample.CustomEvents; [EventHandler("oncustompaste", typeof(CustomPasteEventArgs), enableStopPropagation: true, enablePreventDefault: true)] public static class EventHandlers { } public class CustomPasteEventArgs : EventArgs { public DateTime EventTimestamp { get; set; } public string? PastedData { get; set; } } ``` Add JavaScript code to supply data for the subclass with the preceding handler in a [JavaScript initializer](xref:blazor/fundamentals/startup#javascript-initializers). The following example only handles pasting text, but you could use arbitrary JavaScript APIs to deal with users pasting other types of data, such as images. `wwwroot/{PACKAGE ID/ASSEMBLY NAME}.lib.module.js`: :::moniker-end :::moniker range=">= aspnetcore-8.0" For a Blazor Web App: ```javascript export function afterWebStarted(blazor) { blazor.registerCustomEventType('custompaste', { browserEventName: 'paste', createEventArgs: event => { return { eventTimestamp: new Date(), pastedData: event.clipboardData.getData('text') }; } }); } ``` For a Blazor Server or Blazor WebAssembly app: :::moniker-end :::moniker range=">= aspnetcore-6.0" ```javascript export function afterStarted(blazor) { blazor.registerCustomEventType('custompaste', { browserEventName: 'paste', createEventArgs: event => { return { eventTimestamp: new Date(), pastedData: event.clipboardData.getData('text') }; } }); } ``` In the preceding example, the `{PACKAGE ID/ASSEMBLY NAME}` placeholder of the file name represents the package ID or assembly name of the app. > [!NOTE] > For the call to `registerCustomEventType`, use the `blazor` parameter (lowercase `b`) provided by the Blazor start event. Although the registration is valid when using the `Blazor` object (uppercase `B`), the preferred approach is to use the parameter. The preceding code tells the browser that when a native [`paste`](https://developer.mozilla.org/docs/Web/API/Element/paste_event) event occurs: * Raise a `custompaste` event. * Supply the event arguments data using the custom logic stated: * For the `eventTimestamp`, create a new date. * For the `pastedData`, get the clipboard data as text. For more information, see [MDN Web Docs: ClipboardEvent.clipboardData](https://developer.mozilla.org/docs/Web/API/ClipboardEvent/clipboardData). Event name conventions differ between .NET and JavaScript: * In .NET, event names are prefixed with "`on`". * In JavaScript, event names don't have a prefix. In a Razor component, attach the custom handler to an element. `CustomPasteArguments.razor`: ```razor @page "/custom-paste-arguments" @using BlazorSample.CustomEvents

@message

@code { private string? message; private void HandleCustomPaste(CustomPasteEventArgs eventArgs) { message = $"At {eventArgs.EventTimestamp.ToShortTimeString()}, " + $"you pasted: {eventArgs.PastedData}"; } } ``` :::moniker-end ## Lambda expressions [Lambda expressions](/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions) are supported as the delegate event handler. :::moniker range=">= aspnetcore-9.0" `EventHandler4.razor`: :::code language="razor" source="~/../blazor-samples/9.0/BlazorSample_BlazorWebApp/Components/Pages/EventHandler4.razor"::: :::moniker-end :::moniker range=">= aspnetcore-8.0 < aspnetcore-9.0" `EventHandler4.razor`: :::code language="razor" source="~/../blazor-samples/8.0/BlazorSample_BlazorWebApp/Components/Pages/EventHandler4.razor"::: :::moniker-end :::moniker range=">= aspnetcore-7.0 < aspnetcore-8.0" `EventHandlerExample4.razor`: :::code language="razor" source="~/../blazor-samples/7.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample4.razor"::: :::moniker-end :::moniker range=">= aspnetcore-6.0 < aspnetcore-7.0" `EventHandlerExample4.razor`: :::code language="razor" source="~/../blazor-samples/6.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample4.razor"::: :::moniker-end :::moniker range=">= aspnetcore-5.0 < aspnetcore-6.0" `EventHandlerExample4.razor`: :::code language="razor" source="~/../blazor-samples/5.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample4.razor"::: :::moniker-end :::moniker range="< aspnetcore-5.0" `EventHandlerExample4.razor`: :::code language="razor" source="~/../blazor-samples/3.1/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample4.razor"::: :::moniker-end It's often convenient to close over additional values using C# method parameters, such as when iterating over a set of elements. The following example creates three buttons, each of which calls `UpdateHeading` and passes the following data: * An event argument () in `e`. * The button number in `buttonNumber`. :::moniker range=">= aspnetcore-9.0" `EventHandler5.razor`: :::code language="razor" source="~/../blazor-samples/9.0/BlazorSample_BlazorWebApp/Components/Pages/EventHandler5.razor"::: :::moniker-end :::moniker range=">= aspnetcore-8.0 < aspnetcore-9.0" `EventHandler5.razor`: :::code language="razor" source="~/../blazor-samples/8.0/BlazorSample_BlazorWebApp/Components/Pages/EventHandler5.razor"::: :::moniker-end :::moniker range=">= aspnetcore-7.0 < aspnetcore-8.0" `EventHandlerExample5.razor`: :::code language="razor" source="~/../blazor-samples/7.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample5.razor"::: :::moniker-end :::moniker range=">= aspnetcore-6.0 < aspnetcore-7.0" `EventHandlerExample5.razor`: :::code language="razor" source="~/../blazor-samples/6.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample5.razor"::: :::moniker-end :::moniker range=">= aspnetcore-5.0 < aspnetcore-6.0" `EventHandlerExample5.razor`: :::code language="razor" source="~/../blazor-samples/5.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample5.razor"::: :::moniker-end :::moniker range="< aspnetcore-5.0" `EventHandlerExample5.razor`: :::code language="razor" source="~/../blazor-samples/3.1/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample5.razor"::: :::moniker-end Creating a large number of event delegates in a loop may cause poor rendering performance. For more information, see . Avoid using a loop variable directly in a lambda expression, such as `i` in the preceding `for` loop example. Otherwise, the same variable is used by all lambda expressions, which results in use of the same value in all lambdas. Capture the variable's value in a local variable. In the preceding example: * The loop variable `i` is assigned to `buttonNumber`. * `buttonNumber` is used in the lambda expression. Alternatively, use a `foreach` loop with , which doesn't suffer from the preceding problem: ```razor @foreach (var buttonNumber in Enumerable.Range(1, 3)) {

} ``` ## EventCallback A common scenario with nested components is executing a method in a parent component when a child component event occurs. An `onclick` event occurring in the child component is a common use case. To expose events across components, use an . A parent component can assign a callback method to a child component's . The following `Child` component demonstrates how a button's `onclick` handler is set up to receive an delegate from the sample's `ParentComponent`. The is typed with , which is appropriate for an `onclick` event from a peripheral device. `Child.razor`: :::moniker range=">= aspnetcore-9.0" :::code language="razor" source="~/../blazor-samples/9.0/BlazorSample_BlazorWebApp/Components/Child.razor"::: :::moniker-end :::moniker range=">= aspnetcore-8.0 < aspnetcore-9.0" :::code language="razor" source="~/../blazor-samples/8.0/BlazorSample_BlazorWebApp/Components/Child.razor"::: :::moniker-end :::moniker range=">= aspnetcore-7.0 < aspnetcore-8.0" :::code language="razor" source="~/../blazor-samples/7.0/BlazorSample_WebAssembly/Shared/event-handling/Child.razor"::: :::moniker-end :::moniker range=">= aspnetcore-6.0 < aspnetcore-7.0" :::code language="razor" source="~/../blazor-samples/6.0/BlazorSample_WebAssembly/Shared/event-handling/Child.razor"::: :::moniker-end :::moniker range=">= aspnetcore-5.0 < aspnetcore-6.0" :::code language="razor" source="~/../blazor-samples/5.0/BlazorSample_WebAssembly/Shared/event-handling/Child.razor"::: :::moniker-end :::moniker range="< aspnetcore-5.0" :::code language="razor" source="~/../blazor-samples/3.1/BlazorSample_WebAssembly/Shared/event-handling/Child.razor"::: :::moniker-end The parent component sets the child's (`OnClickCallback`) to its `ShowMessage` method. :::moniker range=">= aspnetcore-9.0" `ParentChild.razor`: :::code language="razor" source="~/../blazor-samples/9.0/BlazorSample_BlazorWebApp/Components/Pages/ParentChild.razor"::: :::moniker-end :::moniker range=">= aspnetcore-8.0 < aspnetcore-9.0" `ParentChild.razor`: :::code language="razor" source="~/../blazor-samples/8.0/BlazorSample_BlazorWebApp/Components/Pages/ParentChild.razor"::: :::moniker-end :::moniker range=">= aspnetcore-7.0 < aspnetcore-8.0" `Parent.razor`: :::code language="razor" source="~/../blazor-samples/7.0/BlazorSample_WebAssembly/Pages/event-handling/Parent.razor"::: :::moniker-end :::moniker range=">= aspnetcore-6.0 < aspnetcore-7.0" `Parent.razor`: :::code language="razor" source="~/../blazor-samples/6.0/BlazorSample_WebAssembly/Pages/event-handling/Parent.razor"::: :::moniker-end :::moniker range=">= aspnetcore-5.0 < aspnetcore-6.0" `Parent.razor`: :::code language="razor" source="~/../blazor-samples/5.0/BlazorSample_WebAssembly/Pages/event-handling/Parent.razor"::: :::moniker-end :::moniker range="< aspnetcore-5.0" `Parent.razor`: :::code language="razor" source="~/../blazor-samples/3.1/BlazorSample_WebAssembly/Pages/event-handling/Parent.razor"::: :::moniker-end When the button is selected in the `ChildComponent`: * The `Parent` component's `ShowMessage` method is called. `message` is updated and displayed in the `Parent` component. * A call to [`StateHasChanged`](xref:blazor/components/lifecycle#state-changes-statehaschanged) isn't required in the callback's method (`ShowMessage`). is called automatically to rerender the `Parent` component, just as child events trigger component rerendering in event handlers that execute within the child. For more information, see . Use and for event handling and binding component parameters. Prefer the strongly typed over . provides enhanced error feedback when an inappropriate type is used, guiding users of the component towards correct implementation. Similar to other UI event handlers, specifying the event parameter is optional. Use when there's no value passed to the callback. and permit asynchronous delegates. is weakly typed and allows passing any type argument in `InvokeAsync(Object)`. is strongly typed and requires passing a `T` argument in `InvokeAsync(T)` that's assignable to `TValue`. Invoke an or with and await the : ```csharp await OnClickCallback.InvokeAsync({ARGUMENT}); ``` In the preceding example, the `{ARGUMENT}` placeholder is an optional argument. The following parent-child example demonstrates the technique. `Child2.razor`: ```razor

Child2 Component

@code { [Parameter] public EventCallback OnClickCallback { get; set; } private async Task TriggerEvent() { await OnClickCallback.InvokeAsync("Blaze It!"); } } ``` `ParentChild2.razor`: ```razor @page "/parent-child-2" Parent Child 2

Parent Child 2 Example

@message1
@message2
@code { private string message1 = string.Empty; private string message2 = string.Empty; } ``` The second occurrence of the `Child2` component demonstrates an asynchronous callback, and the new `message2` value is assigned and rendered with a delay of two seconds. ## Prevent default actions Use the [`@on{DOM EVENT}:preventDefault`](xref:mvc/views/razor#oneventpreventdefault) directive attribute to prevent the default action for an event, where the `{DOM EVENT}` placeholder is a [DOM event](https://developer.mozilla.org/docs/Web/Events). When a key is selected on an input device and the element focus is on a text box, a browser normally displays the key's character in the text box. In the following example, the default behavior is prevented by specifying the `@onkeydown:preventDefault` directive attribute. When the focus is on the `` element, the counter increments with the key sequence Shift++. The `+` character isn't assigned to the `` element's value. For more information on `keydown`, see [`MDN Web Docs: Document: keydown` event](https://developer.mozilla.org/docs/Web/API/Document/keydown_event). :::moniker range=">= aspnetcore-9.0" `EventHandler6.razor`: :::code language="razor" source="~/../blazor-samples/9.0/BlazorSample_BlazorWebApp/Components/Pages/EventHandler6.razor"::: :::moniker-end :::moniker range=">= aspnetcore-8.0 < aspnetcore-9.0" `EventHandler6.razor`: :::code language="razor" source="~/../blazor-samples/8.0/BlazorSample_BlazorWebApp/Components/Pages/EventHandler6.razor"::: :::moniker-end :::moniker range=">= aspnetcore-7.0 < aspnetcore-8.0" `EventHandlerExample6.razor`: :::code language="razor" source="~/../blazor-samples/7.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample6.razor"::: :::moniker-end :::moniker range=">= aspnetcore-6.0 < aspnetcore-7.0" `EventHandlerExample6.razor`: :::code language="razor" source="~/../blazor-samples/6.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample6.razor"::: :::moniker-end :::moniker range=">= aspnetcore-5.0 < aspnetcore-6.0" `EventHandlerExample6.razor`: :::code language="razor" source="~/../blazor-samples/5.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample6.razor"::: :::moniker-end :::moniker range="< aspnetcore-5.0" `EventHandlerExample6.razor`: :::code language="razor" source="~/../blazor-samples/3.1/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample6.razor"::: :::moniker-end Specifying the `@on{DOM EVENT}:preventDefault` attribute without a value is equivalent to `@on{DOM EVENT}:preventDefault="true"`. An expression is also a permitted value of the attribute. In the following example, `shouldPreventDefault` is a `bool` field set to either `true` or `false`: ```razor ... @code { private bool shouldPreventDefault = true; } ``` ## Stop event propagation Use the [`@on{DOM EVENT}:stopPropagation`](xref:mvc/views/razor#oneventstoppropagation) directive attribute to stop event propagation within the Blazor scope. `{DOM EVENT}` is a placeholder for a [DOM event](https://developer.mozilla.org/docs/Web/Events). The `stopPropagation` directive attribute's effect is limited to the Blazor scope and doesn't extend to the HTML DOM. Events must propagate to the HTML DOM root before Blazor can act upon them. For a mechanism to prevent HTML DOM event propagation, consider the following approach: * Obtain the event's path by calling [`Event.composedPath()`](https://developer.mozilla.org/docs/Web/API/Event/composedPath). * Filter events based on the composed [event targets (`EventTarget`)](https://developer.mozilla.org/docs/Web/API/EventTarget). In the following example, selecting the checkbox prevents click events from the second child `
` from propagating to the parent `
`. Since propagated click events normally fire the `OnSelectParentDiv` method, selecting the second child `
` results in the parent `
` message appearing unless the checkbox is selected. :::moniker range=">= aspnetcore-9.0" `EventHandler7.razor`: :::code language="razor" source="~/../blazor-samples/9.0/BlazorSample_BlazorWebApp/Components/Pages/EventHandler7.razor"::: :::moniker-end :::moniker range=">= aspnetcore-8.0 < aspnetcore-9.0" `EventHandler7.razor`: :::code language="razor" source="~/../blazor-samples/8.0/BlazorSample_BlazorWebApp/Components/Pages/EventHandler7.razor"::: :::moniker-end :::moniker range=">= aspnetcore-7.0 < aspnetcore-8.0" `EventHandlerExample7.razor`: :::code language="razor" source="~/../blazor-samples/7.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample7.razor"::: :::moniker-end :::moniker range=">= aspnetcore-6.0 < aspnetcore-7.0" `EventHandlerExample7.razor`: :::code language="razor" source="~/../blazor-samples/6.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample7.razor"::: :::moniker-end :::moniker range=">= aspnetcore-5.0 < aspnetcore-6.0" `EventHandlerExample7.razor`: :::code language="razor" source="~/../blazor-samples/5.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample7.razor"::: :::moniker-end :::moniker range="< aspnetcore-5.0" `EventHandlerExample7.razor`: :::code language="razor" source="~/../blazor-samples/3.1/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample7.razor"::: :::moniker-end :::moniker range=">= aspnetcore-6.0" ## Focus an element Call on an [element reference](xref:blazor/js-interop/call-javascript-from-dotnet#capture-references-to-elements) to focus an element in code. In the following example, select the button to focus the `` element. :::moniker-end :::moniker range=">= aspnetcore-9.0" `EventHandler8.razor`: :::code language="razor" source="~/../blazor-samples/9.0/BlazorSample_BlazorWebApp/Components/Pages/EventHandler8.razor"::: :::moniker-end :::moniker range=">= aspnetcore-8.0 < aspnetcore-9.0" `EventHandler8.razor`: :::code language="razor" source="~/../blazor-samples/8.0/BlazorSample_BlazorWebApp/Components/Pages/EventHandler8.razor"::: :::moniker-end :::moniker range=">= aspnetcore-7.0 < aspnetcore-8.0" `EventHandlerExample8.razor`: :::code language="razor" source="~/../blazor-samples/7.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample8.razor"::: :::moniker-end :::moniker range=">= aspnetcore-6.0 < aspnetcore-7.0" `EventHandlerExample8.razor`: :::code language="razor" source="~/../blazor-samples/6.0/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample8.razor"::: :::moniker-end