From 170bc015914d3643341243b00888a58c887ca377 Mon Sep 17 00:00:00 2001 From: guardrex <1622880+guardrex@users.noreply.github.com> Date: Thu, 11 Mar 2021 08:34:47 -0600 Subject: [PATCH 1/5] Blazor Event Handling UE Pass --- .../blazor/components/event-handling.md | 283 +++++++++++++----- 1 file changed, 206 insertions(+), 77 deletions(-) diff --git a/aspnetcore/blazor/components/event-handling.md b/aspnetcore/blazor/components/event-handling.md index 451a1aa44283..36764e3564ee 100644 --- a/aspnetcore/blazor/components/event-handling.md +++ b/aspnetcore/blazor/components/event-handling.md @@ -5,67 +5,127 @@ description: Learn about Blazor's event handling features, including event argum monikerRange: '>= aspnetcore-3.1' ms.author: riande ms.custom: mvc -ms.date: 10/20/2020 +ms.date: 03/11/2021 no-loc: [appsettings.json, "ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR] uid: blazor/components/event-handling --- # ASP.NET Core Blazor event handling -Razor components provide event handling features. For an HTML element attribute named [`@on{EVENT}`](xref:mvc/views/razor#onevent) (for example, `@onclick`) with a delegate-typed value, a Razor component treats the attribute's value as an event handler. +Specify delegate event handlers in Razor component markup with [`@on{EVENT}={DELEGATE}`](xref:mvc/views/razor#onevent) with the following placeholder values: -The following code calls the `UpdateHeading` method when the button is selected in the UI: +* `{EVENT}` is a [Document Object Model (DOM)](https://developer.mozilla.org/docs/Web/API/Document_Object_Model/Introduction) event (for example, `@onclick`). +* `{DELEGATE}` is the delegate-typed value. -```razor - +The following code: -@code { - private void UpdateHeading(MouseEventArgs e) - { - ... - } -} -``` +* Calls the `UpdateHeading` method when the button is selected in the UI. +* Calls the `CheckChanged` method when the check box is changed in the UI. -The following code calls the `CheckChanged` method when the check box is changed in the UI: +`Pages/EventHandlerExample1.razor`: ```razor - +@page "/event-handler-example-1" + +

@currentHeading

+ +

+ + +

+ +

+ +

@code { + private string currentHeading = "Initial heading"; + private string newHeading; + private string checkedMessage = "Not changed yet"; + + private void UpdateHeading() + { + currentHeading = $"{newHeading}!!!"; + } + private void CheckChanged() { - ... + checkedMessage = $"Last changed at {DateTime.Now}"; } } ``` Event handlers can also be asynchronous and return a . There's no need to manually call [StateHasChanged](xref:blazor/components/lifecycle#state-changes). Exceptions are logged when they occur. -In the following example, `UpdateHeading` is called asynchronously when the button is selected: +In the following example, `UpdateHeading`: + +* Is called asynchronously when the button is selected. +* Waits two seconds before updating the heading. + +`Pages/EventHandlerExample2.razor`: ```razor - +@page "/event-handler-example-2" + +

@currentHeading

+ +

+ + +

@code { - private async Task UpdateHeading(MouseEventArgs e) + private string currentHeading = "Initial heading"; + private string newHeading; + + private async Task UpdateHeading() { - await ... + await Task.Delay(2000); + + currentHeading = $"{newHeading}!!!"; } } ``` ## Event argument types -For some events, event argument types are permitted. Specifying an event parameter in an event method definition is optional and only necessary if the event type is used in the method. In the following example, the `MouseEventArgs` event argument is used in the `ShowMessage` method to set message text: +For some events, event argument types are permitted. Specifying an event parameter in an event method definition is only necessary if the event type is used in the method. In the following example, the `MouseEventArgs` event argument is used in the `ReportPointerLocation` method to set message text. -```csharp -private void ShowMessage(MouseEventArgs e) +`Pages/EventHandlerExample3.razor`: + +```razor +@page "/event-handler-example-3" + +@for (var i = 0; i < 4; i++) { - messageText = $"The mouse is at coordinates: {e.ScreenX}:{e.ScreenY}"; +

+ +

+} + +

@mousePointerMessage

+ +@code { + private string mousePointerMessage; + + private void ReportPointerLocation(MouseEventArgs e) + { + mousePointerMessage = $"Mouse coordinates: {e.ScreenX}:{e.ScreenY}"; + } } ``` @@ -73,8 +133,8 @@ Supported are shown in the following table. ::: moniker range=">= aspnetcore-5.0" -| Event | Class | DOM events and notes | -| ---------------- | ------ | -------------------- | +| Event | Class | [Document Object Model (DOM)](https://developer.mozilla.org/docs/Web/API/Document_Object_Model/Introduction) events and notes | +| ---------------- | ------ | --- | | Clipboard | | `oncut`, `oncopy`, `onpaste` | | Drag | | `ondrag`, `ondragstart`, `ondragenter`, `ondragleave`, `ondragover`, `ondrop`, `ondragend`

and hold dragged item data.

Implement drag and drop in Blazor apps using [JS interop](xref:blazor/call-javascript-from-dotnet) with [HTML Drag and Drop API](https://developer.mozilla.org/docs/Web/API/HTML_Drag_and_Drop_API). | | Error | | `onerror` | @@ -92,8 +152,8 @@ Supported are shown in the following table. ::: moniker range="< aspnetcore-5.0" -| Event | Class | DOM events and notes | -| ---------------- | ----- | -------------------- | +| Event | Class | [Document Object Model (DOM)](https://developer.mozilla.org/docs/Web/API/Document_Object_Model/Introduction) events and notes | +| ---------------- | ----- | --- | | Clipboard | | `oncut`, `oncopy`, `onpaste` | | Drag | | `ondrag`, `ondragstart`, `ondragenter`, `ondragleave`, `ondragover`, `ondrop`, `ondragend`

and hold dragged item data.

Implement drag and drop in Blazor apps using [JS interop](xref:blazor/call-javascript-from-dotnet) with [HTML Drag and Drop API](https://developer.mozilla.org/docs/Web/API/HTML_Drag_and_Drop_API). | | Error | | `onerror` | @@ -119,80 +179,118 @@ For more information, see the following resources: ## Lambda expressions -[Lambda expressions](/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions) can also be used: +[Lambda expressions](/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions) can also be used as the delegate event handler. + +`Pages/EventHandlerExample4.razor`: ```razor - +@page "/event-handler-example-4" + +

@heading

+ +

+ +

+ +@code { + private string heading = "Initial heading"; +} ``` -It's often convenient to close over additional values, such as when iterating over a set of elements. The following example creates three buttons, each of which calls `UpdateHeading` passing an event argument () and its button number (`buttonNumber`) when selected in the UI: +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` passing an event argument () in `e` and its button number in `buttonNumber` when selected in the UI. + +`Pages/EventHandlerExample5.razor`: ```razor -

@message

+@page "/event-handler-example-5" + +

@heading

@for (var i = 1; i < 4; i++) { var buttonNumber = i; - +

+ +

} @code { - private string message = "Select a button to learn its position."; + private string heading = "Select a button to learn its position"; private void UpdateHeading(MouseEventArgs e, int buttonNumber) { - message = $"You selected Button #{buttonNumber} at " + - $"mouse position: {e.ClientX} X {e.ClientY}."; + heading = $"Selected #{buttonNumber} at {e.ClientX}:{e.ClientY}"; } } ``` > [!NOTE] -> Do **not** use 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. Always capture the variable's value in a local variable and then use it. In the preceding example, the loop variable `i` is assigned to `buttonNumber`. +> Do **not** use 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. Always capture the variable's value in a local variable and then use it. In the preceding example: +> +> * The loop variable `i` is assigned to `buttonNumber`. +> * `buttonNumber` is used in the lambda expression. ## EventCallback -A common scenario with nested components is the desire to run a parent component's method 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 . +A common scenario with nested components executes a parent component's method 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 `MouseEventArgs`, which is appropriate for an `onclick` event from a peripheral device. -The `ChildComponent` in the sample app (`Components/ChildComponent.razor`) demonstrates how a button's `onclick` handler is set up to receive an delegate from the sample's `ParentComponent`. The is typed with `MouseEventArgs`, which is appropriate for an `onclick` event from a peripheral device: +`Shared/Child.razor`: + +```razor +

+ +

-[!code-razor[](~/blazor/common/samples/5.x/BlazorWebAssemblySample/Components/ChildComponent.razor?highlight=5-7,17-18)] +@code { + [Parameter] + public string Title { get; set; } -The `ParentComponent` sets the child's (`OnClickCallback`) to its `ShowMessage` method. + [Parameter] + public RenderFragment ChildContent { get; set; } -`Pages/ParentComponent.razor`: + [Parameter] + public EventCallback OnClickCallback { get; set; } +} +``` + +The `Parent` component sets the child's (`OnClickCallback`) to its `ShowMessage` method. + +`Pages/Parent.razor`: ```razor -@page "/ParentComponent" +@page "/parent"

Parent-child example

- - Content of the child component is supplied - by the parent component. - + + Content of the child component is supplied by the parent component. + -

@messageText

+

@message

@code { - private string messageText; + private string message; private void ShowMessage(MouseEventArgs e) { - messageText = $"Blaze a new trail with Blazor! ({e.ScreenX}, {e.ScreenY})"; + message = $"Blaze a new trail with Blazor! ({e.ScreenX}:{e.ScreenY})"; } } ``` When the button is selected in the `ChildComponent`: -* The `ParentComponent`'s `ShowMessage` method is called. `messageText` is updated and displayed in the `ParentComponent`. -* A call to [`StateHasChanged`](xref:blazor/components/lifecycle#state-changes) isn't required in the callback's method (`ShowMessage`). is called automatically to rerender the `ParentComponent`, just as child events trigger component rerendering in event handlers that execute within the child. For more information, see . +* 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) 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 . 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`. @@ -209,16 +307,22 @@ await OnClickCallback.InvokeAsync(arg); Use and for event handling and binding component parameters. -Prefer the strongly typed over . provides better error feedback to users of the component. Similar to other UI event handlers, specifying the event parameter is optional. Use when there's no value passed to the callback. +Prefer the strongly typed over . provides enhanced error feedback to users of the component. Similar to other UI event handlers, specifying the event parameter is optional. Use when there's no value passed to the callback. ## Prevent default actions -Use the [`@on{EVENT}:preventDefault`](xref:mvc/views/razor#oneventpreventdefault) directive attribute to prevent the default action for an event. +Use the [`@on{EVENT}:preventDefault`](xref:mvc/views/razor#oneventpreventdefault) directive attribute to prevent the default action for an event, where the `{EVENT}` placeholder is a [Document Object Model (DOM)](https://developer.mozilla.org/docs/Web/API/Document_Object_Model/Introduction) event. -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 `@onkeypress:preventDefault` directive attribute. The counter increments, and the **+** key isn't captured into the `` element's value: +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 ([`keydown`](https://developer.mozilla.org/docs/Web/API/Document/keydown_event)). 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. + +`Pages/EventHandlerExample6.razor`: ```razor - +@page "/event-handler-example-6" + +

+ +

@code { private int count = 0; @@ -238,40 +342,57 @@ Specifying the `@on{EVENT}:preventDefault` attribute without a value is equivale The value of the attribute can also be an expression. 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{EVENT}:stopPropagation`](xref:mvc/views/razor#oneventstoppropagation) directive attribute to stop event propagation. +Use the [`@on{EVENT}:stopPropagation`](xref:mvc/views/razor#oneventstoppropagation) directive attribute to stop event propagation, where the `{EVENT}` placeholder is a [Document Object Model (DOM)](https://developer.mozilla.org/docs/Web/API/Document_Object_Model/Introduction) event. -In the following example, selecting the check box prevents click events from the second child `
` from propagating to the parent `
`: +In the following example, selecting the check box 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 div message appearing unless the check box is selected. + +`Pages/EventHandlerExample7.razor`: ```razor +@page "/event-handler-example-7" + -
+

Parent div

-
+
Child div that doesn't stop propagation when selected.
-
+
Child div that stops propagation when selected.
+

+ @message +

+ @code { private bool stopPropagation = false; + private string message; + + private void OnSelectParentDiv() => + message = $"The parent div was selected. {DateTime.Now}"; - private void OnSelectParentDiv() => - Console.WriteLine($"The parent div was selected. {DateTime.Now}"); - private void OnSelectChildDiv() => - Console.WriteLine($"A child div was selected. {DateTime.Now}"); + private void OnSelectChildDiv() => + message = $"A child div was selected. {DateTime.Now}"; } ``` @@ -279,16 +400,24 @@ In the following example, selecting the check box prevents click events from the ## Focus an element -Call `FocusAsync` on an [element reference](xref:blazor/call-javascript-from-dotnet#capture-references-to-elements) to focus an element in code: +Call on an [element reference](xref:blazor/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. + +`Pages/EventHandlerExample8.razor`: ```razor - +@page "/event-handler-example-8" + +

+ +

- + @code { private ElementReference exampleInput; - + private async Task ChangeFocus() { await exampleInput.FocusAsync(); From a26c2e41cd98469f04017d08972e021d90424e3e Mon Sep 17 00:00:00 2001 From: guardrex <1622880+guardrex@users.noreply.github.com> Date: Fri, 12 Mar 2021 05:28:26 -0600 Subject: [PATCH 2/5] Add custom event args for 6.0 --- .../blazor/components/event-handling.md | 154 +++++++++++++++++- 1 file changed, 153 insertions(+), 1 deletion(-) diff --git a/aspnetcore/blazor/components/event-handling.md b/aspnetcore/blazor/components/event-handling.md index 36764e3564ee..d7543cd41c19 100644 --- a/aspnetcore/blazor/components/event-handling.md +++ b/aspnetcore/blazor/components/event-handling.md @@ -99,7 +99,13 @@ In the following example, `UpdateHeading`: } ``` -## Event argument types +## Event arguments + +::: moniker range=">= aspnetcore-6.0" + +### Built-in event arguments + +::: moniker-end For some events, event argument types are permitted. Specifying an event parameter in an event method definition is only necessary if the event type is used in the method. In the following example, the `MouseEventArgs` event argument is used in the `ReportPointerLocation` method to set message text. @@ -177,6 +183,152 @@ For more information, see the following resources: * [MDN web docs: GlobalEventHandlers](https://developer.mozilla.org/docs/Web/API/GlobalEventHandlers): Includes information on which HTML elements support each DOM event. +::: 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. + +1. 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.value + }; + } + ``` + +1. Register the custom event with the above created handler: + + ```javascript + Blazor.registerCustomEventType('customevent', { + createEventArgs: eventArgsCreator; + ``` + + > [!NOTE] + > The call to `registerCustomEventType` is performed in a JavaScript only once per event. + +1. Define a class for the event args: + + ```csharp + public class CustomEventArgs : EventArgs{ + public string CustomProperty1 {get; set;} + public string CustomProperty2 {get; set;} + } + ``` + +1. Wire up the custom event with the event args by adding an attribute annotation for the custom event. The class doesn't require any members: + + ```csharp + [EventHandler("oncustomevent", typeof(CustomEventArgs), enableStopPropagation: true, enablePreventDefault: true)] + static class EventHandlers + { + } + ``` + +1. Register the event handler on one or more HTML elements. Access the data that was passed in from Javascript in the `HandleCustomEvent` method: + + ```razor + + + @code + { + void HandleCustomEvent(CustomEventArgs eventArgs) + { + // eventArgs.CustomProperty1; + // eventArgs.CustomProperty2; + } + } + ``` + +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 method. 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 +[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. In the `wwwroot/index.html` or `Pages/_Host.cshtml` file, add the following ` +``` + +The preceding code tells the browser that when a native paste event occurs: + +* Raise a `custompaste` event. +* Supply the event arguments data using your custom logic. + +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. + +`Pages/CustomPasteArguments.razor`: + +```razor +@page "/custom-paste-arguments" + + + +

+ @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) can also be used as the delegate event handler. From 579a4b1e92481852ac9ea1cf9985f15a8147515d Mon Sep 17 00:00:00 2001 From: guardrex <1622880+guardrex@users.noreply.github.com> Date: Mon, 15 Mar 2021 07:26:20 -0500 Subject: [PATCH 3/5] Updates --- .../blazor/components/event-handling.md | 61 +++++++++++-------- aspnetcore/release-notes/aspnetcore-5.0.md | 2 +- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/aspnetcore/blazor/components/event-handling.md b/aspnetcore/blazor/components/event-handling.md index d7543cd41c19..157455fb452b 100644 --- a/aspnetcore/blazor/components/event-handling.md +++ b/aspnetcore/blazor/components/event-handling.md @@ -5,16 +5,22 @@ description: Learn about Blazor's event handling features, including event argum monikerRange: '>= aspnetcore-3.1' ms.author: riande ms.custom: mvc -ms.date: 03/11/2021 +ms.date: 03/15/2021 no-loc: [appsettings.json, "ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR] uid: blazor/components/event-handling --- # ASP.NET Core Blazor event handling -Specify delegate event handlers in Razor component markup with [`@on{EVENT}={DELEGATE}`](xref:mvc/views/razor#onevent) with the following placeholder values: +Specify delegate event handlers in Razor component markup with [`@on{DOM EVENT}="{DELEGATE}"`](xref:mvc/views/razor#onevent) Razor syntax: -* `{EVENT}` is a [Document Object Model (DOM)](https://developer.mozilla.org/docs/Web/API/Document_Object_Model/Introduction) event (for example, `@onclick`). -* `{DELEGATE}` is the delegate-typed value. +* The `{DOM EVENT}` placeholder is a [Document Object Model (DOM) event](https://developer.mozilla.org/docs/Web/Events) (for example, `onclick`). +* The `{DELEGATE}` placeholder is the C# delegate handler. + +Event handling characteristics include the following: + +* 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). +* Exceptions are logged. The following code: @@ -62,8 +68,6 @@ The following code: } ``` -Event handlers can also be asynchronous and return a . There's no need to manually call [StateHasChanged](xref:blazor/components/lifecycle#state-changes). Exceptions are logged when they occur. - In the following example, `UpdateHeading`: * Is called asynchronously when the button is selected. @@ -107,7 +111,7 @@ In the following example, `UpdateHeading`: ::: moniker-end -For some events, event argument types are permitted. Specifying an event parameter in an event method definition is only necessary if the event type is used in the method. In the following example, the `MouseEventArgs` event argument is used in the `ReportPointerLocation` method to set message text. +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. `Pages/EventHandlerExample3.razor`: @@ -204,26 +208,30 @@ Custom events with custom event arguments are generally enabled with the followi } ``` -1. Register the custom event with the above created handler: +1. Register the custom event with the preceding handler in `wwwroot/index.html` (Blazor WebAssembly) or `Pages/_Host.cshtml` (Blazor Server) immediately after the Blazor script: - ```javascript - Blazor.registerCustomEventType('customevent', { - createEventArgs: eventArgsCreator; + ```html + ``` > [!NOTE] - > The call to `registerCustomEventType` is performed in a JavaScript only once per event. + > The call to `registerCustomEventType` is performed in a script only once per event. -1. Define a class for the event args: +1. Define a class for the event arguments: ```csharp - public class CustomEventArgs : EventArgs{ + public class CustomEventArgs : EventArgs + { public string CustomProperty1 {get; set;} public string CustomProperty2 {get; set;} } ``` -1. Wire up the custom event with the event args by adding an attribute annotation for the custom event. The class doesn't require any members: +1. Wire up the custom event with the event arguments by adding an attribute annotation for the custom event. The class doesn't require members: ```csharp [EventHandler("oncustomevent", typeof(CustomEventArgs), enableStopPropagation: true, enablePreventDefault: true)] @@ -232,7 +240,7 @@ Custom events with custom event arguments are generally enabled with the followi } ``` -1. Register the event handler on one or more HTML elements. Access the data that was passed in from Javascript in the `HandleCustomEvent` method: +1. 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 @@ -241,8 +249,8 @@ Custom events with custom event arguments are generally enabled with the followi { void HandleCustomEvent(CustomEventArgs eventArgs) { - // eventArgs.CustomProperty1; - // eventArgs.CustomProperty2; + // eventArgs.CustomProperty1 + // eventArgs.CustomProperty2 } } ``` @@ -255,12 +263,13 @@ If you're attempting to fire a custom event, [`bubbles`](https://developer.mozil 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: +Declare a custom name (`oncustompaste`) for the event and a .NET class (`CustomPasteEventArgs`) to hold the event arguments for this event: `CustomEvents.cs`: ```csharp -[EventHandler("oncustompaste", typeof(CustomPasteEventArgs), enableStopPropagation: true, enablePreventDefault: true)] +[EventHandler("oncustompaste", typeof(CustomPasteEventArgs), + enableStopPropagation: true, enablePreventDefault: true)] public static class EventHandlers { } @@ -272,7 +281,7 @@ public class CustomPasteEventArgs : EventArgs } ``` -Add JavaScript code to supply data for the subclass. In the `wwwroot/index.html` or `Pages/_Host.cshtml` file, add the following ` ``` -The preceding code tells the browser that when a native paste event occurs: +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 your custom logic. @@ -463,7 +472,7 @@ Prefer the strongly typed ` element, the counter increments with the key sequence Shift++. The `+` character isn't assigned to the `` element's value. @@ -489,9 +498,9 @@ When a key is selected on an input device and the element focus is on a text box } ``` -Specifying the `@on{EVENT}:preventDefault` attribute without a value is equivalent to `@on{EVENT}:preventDefault="true"`. +Specifying the `@on{DOM EVENT}:preventDefault` attribute without a value is equivalent to `@on{DOM EVENT}:preventDefault="true"`. -The value of the attribute can also be an expression. In the following example, `shouldPreventDefault` is a `bool` field set to either `true` or `false`: +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 @@ -505,7 +514,7 @@ The value of the attribute can also be an expression. In the following example, ## Stop event propagation -Use the [`@on{EVENT}:stopPropagation`](xref:mvc/views/razor#oneventstoppropagation) directive attribute to stop event propagation, where the `{EVENT}` placeholder is a [Document Object Model (DOM)](https://developer.mozilla.org/docs/Web/API/Document_Object_Model/Introduction) event. +Use the [`@on{DOM EVENT}:stopPropagation`](xref:mvc/views/razor#oneventstoppropagation) directive attribute to stop event propagation, where the `{DOM EVENT}` placeholder is a [Document Object Model (DOM) event](https://developer.mozilla.org/docs/Web/Events). In the following example, selecting the check box 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 div message appearing unless the check box is selected. diff --git a/aspnetcore/release-notes/aspnetcore-5.0.md b/aspnetcore/release-notes/aspnetcore-5.0.md index 756b9c14a5da..33a3091ad717 100644 --- a/aspnetcore/release-notes/aspnetcore-5.0.md +++ b/aspnetcore/release-notes/aspnetcore-5.0.md @@ -135,7 +135,7 @@ Improve the perceived performance of component rendering using the Blazor framew ### `ontoggle` event support -Blazor events now support the `ontoggle` DOM event. For more information, see . +Blazor events now support the `ontoggle` DOM event. For more information, see . ### Set UI focus in Blazor apps From c1c9ad723fc72d5d830a1a939827eac198f3510d Mon Sep 17 00:00:00 2001 From: guardrex <1622880+guardrex@users.noreply.github.com> Date: Tue, 16 Mar 2021 09:12:01 -0500 Subject: [PATCH 4/5] Updates --- .../event-handling/EventHandlerExample1.razor | 36 +++ .../event-handling/EventHandlerExample2.razor | 25 ++ .../event-handling/EventHandlerExample3.razor | 21 ++ .../event-handling/EventHandlerExample4.razor | 13 + .../event-handling/EventHandlerExample5.razor | 23 ++ .../event-handling/EventHandlerExample6.razor | 17 ++ .../event-handling/EventHandlerExample7.razor | 34 +++ .../Pages/event-handling/Parent.razor | 18 ++ .../Shared/event-handling/Child.razor | 16 + .../3.x/BlazorSample_Server/_Imports.razor | 1 + .../event-handling/EventHandlerExample1.razor | 36 +++ .../event-handling/EventHandlerExample2.razor | 25 ++ .../event-handling/EventHandlerExample3.razor | 21 ++ .../event-handling/EventHandlerExample4.razor | 13 + .../event-handling/EventHandlerExample5.razor | 23 ++ .../event-handling/EventHandlerExample6.razor | 17 ++ .../event-handling/EventHandlerExample7.razor | 34 +++ .../Pages/event-handling/Parent.razor | 18 ++ .../Shared/event-handling/Child.razor | 16 + .../BlazorSample_WebAssembly/_Imports.razor | 1 + .../event-handling/EventHandlerExample1.razor | 36 +++ .../event-handling/EventHandlerExample2.razor | 25 ++ .../event-handling/EventHandlerExample3.razor | 21 ++ .../event-handling/EventHandlerExample4.razor | 13 + .../event-handling/EventHandlerExample5.razor | 23 ++ .../event-handling/EventHandlerExample6.razor | 17 ++ .../event-handling/EventHandlerExample7.razor | 34 +++ .../event-handling/EventHandlerExample8.razor | 18 ++ .../Pages/event-handling/Parent.razor | 18 ++ .../Shared/event-handling/Child.razor | 16 + .../5.x/BlazorSample_Server/_Imports.razor | 1 + .../event-handling/EventHandlerExample1.razor | 36 +++ .../event-handling/EventHandlerExample2.razor | 25 ++ .../event-handling/EventHandlerExample3.razor | 21 ++ .../event-handling/EventHandlerExample4.razor | 13 + .../event-handling/EventHandlerExample5.razor | 23 ++ .../event-handling/EventHandlerExample6.razor | 17 ++ .../event-handling/EventHandlerExample7.razor | 34 +++ .../event-handling/EventHandlerExample8.razor | 18 ++ .../Pages/event-handling/Parent.razor | 18 ++ .../Shared/event-handling/Child.razor | 16 + .../BlazorSample_WebAssembly/_Imports.razor | 1 + .../blazor/components/event-handling.md | 288 +++++------------- 43 files changed, 928 insertions(+), 212 deletions(-) create mode 100644 aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample1.razor create mode 100644 aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample2.razor create mode 100644 aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample3.razor create mode 100644 aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample4.razor create mode 100644 aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample5.razor create mode 100644 aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample6.razor create mode 100644 aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample7.razor create mode 100644 aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/Parent.razor create mode 100644 aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Shared/event-handling/Child.razor create mode 100644 aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample1.razor create mode 100644 aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample2.razor create mode 100644 aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample3.razor create mode 100644 aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample4.razor create mode 100644 aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample5.razor create mode 100644 aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample6.razor create mode 100644 aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample7.razor create mode 100644 aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/Parent.razor create mode 100644 aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Shared/event-handling/Child.razor create mode 100644 aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample1.razor create mode 100644 aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample2.razor create mode 100644 aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample3.razor create mode 100644 aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample4.razor create mode 100644 aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample5.razor create mode 100644 aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample6.razor create mode 100644 aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample7.razor create mode 100644 aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample8.razor create mode 100644 aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/Parent.razor create mode 100644 aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Shared/event-handling/Child.razor create mode 100644 aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample1.razor create mode 100644 aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample2.razor create mode 100644 aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample3.razor create mode 100644 aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample4.razor create mode 100644 aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample5.razor create mode 100644 aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample6.razor create mode 100644 aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample7.razor create mode 100644 aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample8.razor create mode 100644 aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/Parent.razor create mode 100644 aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Shared/event-handling/Child.razor diff --git a/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample1.razor b/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample1.razor new file mode 100644 index 000000000000..16edc1913d5e --- /dev/null +++ b/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample1.razor @@ -0,0 +1,36 @@ +@page "/event-handler-example-1" + +

@currentHeading

+ +

+ + +

+ +

+ +

+ +@code { + private string currentHeading = "Initial heading"; + private string newHeading; + private string checkedMessage = "Not changed yet"; + + private void UpdateHeading() + { + currentHeading = $"{newHeading}!!!"; + } + + private void CheckChanged() + { + checkedMessage = $"Last changed at {DateTime.Now}"; + } +} diff --git a/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample2.razor b/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample2.razor new file mode 100644 index 000000000000..b4a920afe315 --- /dev/null +++ b/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample2.razor @@ -0,0 +1,25 @@ +@page "/event-handler-example-2" + +

@currentHeading

+ +

+ + +

+ +@code { + private string currentHeading = "Initial heading"; + private string newHeading; + + private async Task UpdateHeading() + { + await Task.Delay(2000); + + currentHeading = $"{newHeading}!!!"; + } +} diff --git a/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample3.razor b/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample3.razor new file mode 100644 index 000000000000..5e6722a08f21 --- /dev/null +++ b/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample3.razor @@ -0,0 +1,21 @@ +@page "/event-handler-example-3" + +@for (var i = 0; i < 4; i++) +{ +

+ +

+} + +

@mousePointerMessage

+ +@code { + private string mousePointerMessage; + + private void ReportPointerLocation(MouseEventArgs e) + { + mousePointerMessage = $"Mouse coordinates: {e.ScreenX}:{e.ScreenY}"; + } +} diff --git a/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample4.razor b/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample4.razor new file mode 100644 index 000000000000..8f934b888858 --- /dev/null +++ b/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample4.razor @@ -0,0 +1,13 @@ +@page "/event-handler-example-4" + +

@heading

+ +

+ +

+ +@code { + private string heading = "Initial heading"; +} diff --git a/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample5.razor b/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample5.razor new file mode 100644 index 000000000000..142713395074 --- /dev/null +++ b/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample5.razor @@ -0,0 +1,23 @@ +@page "/event-handler-example-5" + +

@heading

+ +@for (var i = 1; i < 4; i++) +{ + var buttonNumber = i; + +

+ +

+} + +@code { + private string heading = "Select a button to learn its position"; + + private void UpdateHeading(MouseEventArgs e, int buttonNumber) + { + heading = $"Selected #{buttonNumber} at {e.ClientX}:{e.ClientY}"; + } +} diff --git a/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample6.razor b/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample6.razor new file mode 100644 index 000000000000..f3ebbf3a2e5b --- /dev/null +++ b/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample6.razor @@ -0,0 +1,17 @@ +@page "/event-handler-example-6" + +

+ +

+ +@code { + private int count = 0; + + private void KeyHandler(KeyboardEventArgs e) + { + if (e.Key == "+") + { + count++; + } + } +} diff --git a/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample7.razor b/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample7.razor new file mode 100644 index 000000000000..3a2f10db199c --- /dev/null +++ b/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample7.razor @@ -0,0 +1,34 @@ +@page "/event-handler-example-7" + + + +
+

Parent div

+ +
+ Child div that doesn't stop propagation when selected. +
+ +
+ Child div that stops propagation when selected. +
+
+ +

+ @message +

+ +@code { + private bool stopPropagation = false; + private string message; + + private void OnSelectParentDiv() => + message = $"The parent div was selected. {DateTime.Now}"; + + private void OnSelectChildDiv() => + message = $"A child div was selected. {DateTime.Now}"; +} diff --git a/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/Parent.razor b/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/Parent.razor new file mode 100644 index 000000000000..896aa0cb950d --- /dev/null +++ b/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Pages/event-handling/Parent.razor @@ -0,0 +1,18 @@ +@page "/parent" + +

Parent-child example

+ + + Content of the child component is supplied by the parent component. + + +

@message

+ +@code { + private string message; + + private void ShowMessage(MouseEventArgs e) + { + message = $"Blaze a new trail with Blazor! ({e.ScreenX}:{e.ScreenY})"; + } +} diff --git a/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Shared/event-handling/Child.razor b/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Shared/event-handling/Child.razor new file mode 100644 index 000000000000..2cd0eaed242d --- /dev/null +++ b/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/Shared/event-handling/Child.razor @@ -0,0 +1,16 @@ +

+ +

+ +@code { + [Parameter] + public string Title { get; set; } + + [Parameter] + public RenderFragment ChildContent { get; set; } + + [Parameter] + public EventCallback OnClickCallback { get; set; } +} diff --git a/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/_Imports.razor b/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/_Imports.razor index bda9e18ae1a5..8c18947a0299 100644 --- a/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/_Imports.razor +++ b/aspnetcore/blazor/common/samples/3.x/BlazorSample_Server/_Imports.razor @@ -8,3 +8,4 @@ @using BlazorSample @using BlazorSample.Shared @using BlazorSample.Shared.templated_components +@using BlazorSample.Shared.event_handing diff --git a/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample1.razor b/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample1.razor new file mode 100644 index 000000000000..16edc1913d5e --- /dev/null +++ b/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample1.razor @@ -0,0 +1,36 @@ +@page "/event-handler-example-1" + +

@currentHeading

+ +

+ + +

+ +

+ +

+ +@code { + private string currentHeading = "Initial heading"; + private string newHeading; + private string checkedMessage = "Not changed yet"; + + private void UpdateHeading() + { + currentHeading = $"{newHeading}!!!"; + } + + private void CheckChanged() + { + checkedMessage = $"Last changed at {DateTime.Now}"; + } +} diff --git a/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample2.razor b/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample2.razor new file mode 100644 index 000000000000..b4a920afe315 --- /dev/null +++ b/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample2.razor @@ -0,0 +1,25 @@ +@page "/event-handler-example-2" + +

@currentHeading

+ +

+ + +

+ +@code { + private string currentHeading = "Initial heading"; + private string newHeading; + + private async Task UpdateHeading() + { + await Task.Delay(2000); + + currentHeading = $"{newHeading}!!!"; + } +} diff --git a/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample3.razor b/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample3.razor new file mode 100644 index 000000000000..5e6722a08f21 --- /dev/null +++ b/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample3.razor @@ -0,0 +1,21 @@ +@page "/event-handler-example-3" + +@for (var i = 0; i < 4; i++) +{ +

+ +

+} + +

@mousePointerMessage

+ +@code { + private string mousePointerMessage; + + private void ReportPointerLocation(MouseEventArgs e) + { + mousePointerMessage = $"Mouse coordinates: {e.ScreenX}:{e.ScreenY}"; + } +} diff --git a/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample4.razor b/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample4.razor new file mode 100644 index 000000000000..8f934b888858 --- /dev/null +++ b/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample4.razor @@ -0,0 +1,13 @@ +@page "/event-handler-example-4" + +

@heading

+ +

+ +

+ +@code { + private string heading = "Initial heading"; +} diff --git a/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample5.razor b/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample5.razor new file mode 100644 index 000000000000..142713395074 --- /dev/null +++ b/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample5.razor @@ -0,0 +1,23 @@ +@page "/event-handler-example-5" + +

@heading

+ +@for (var i = 1; i < 4; i++) +{ + var buttonNumber = i; + +

+ +

+} + +@code { + private string heading = "Select a button to learn its position"; + + private void UpdateHeading(MouseEventArgs e, int buttonNumber) + { + heading = $"Selected #{buttonNumber} at {e.ClientX}:{e.ClientY}"; + } +} diff --git a/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample6.razor b/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample6.razor new file mode 100644 index 000000000000..f3ebbf3a2e5b --- /dev/null +++ b/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample6.razor @@ -0,0 +1,17 @@ +@page "/event-handler-example-6" + +

+ +

+ +@code { + private int count = 0; + + private void KeyHandler(KeyboardEventArgs e) + { + if (e.Key == "+") + { + count++; + } + } +} diff --git a/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample7.razor b/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample7.razor new file mode 100644 index 000000000000..3a2f10db199c --- /dev/null +++ b/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample7.razor @@ -0,0 +1,34 @@ +@page "/event-handler-example-7" + + + +
+

Parent div

+ +
+ Child div that doesn't stop propagation when selected. +
+ +
+ Child div that stops propagation when selected. +
+
+ +

+ @message +

+ +@code { + private bool stopPropagation = false; + private string message; + + private void OnSelectParentDiv() => + message = $"The parent div was selected. {DateTime.Now}"; + + private void OnSelectChildDiv() => + message = $"A child div was selected. {DateTime.Now}"; +} diff --git a/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/Parent.razor b/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/Parent.razor new file mode 100644 index 000000000000..896aa0cb950d --- /dev/null +++ b/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/Parent.razor @@ -0,0 +1,18 @@ +@page "/parent" + +

Parent-child example

+ + + Content of the child component is supplied by the parent component. + + +

@message

+ +@code { + private string message; + + private void ShowMessage(MouseEventArgs e) + { + message = $"Blaze a new trail with Blazor! ({e.ScreenX}:{e.ScreenY})"; + } +} diff --git a/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Shared/event-handling/Child.razor b/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Shared/event-handling/Child.razor new file mode 100644 index 000000000000..2cd0eaed242d --- /dev/null +++ b/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/Shared/event-handling/Child.razor @@ -0,0 +1,16 @@ +

+ +

+ +@code { + [Parameter] + public string Title { get; set; } + + [Parameter] + public RenderFragment ChildContent { get; set; } + + [Parameter] + public EventCallback OnClickCallback { get; set; } +} diff --git a/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/_Imports.razor b/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/_Imports.razor index f60fe52c7136..c59b5293e8b2 100644 --- a/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/_Imports.razor +++ b/aspnetcore/blazor/common/samples/3.x/BlazorSample_WebAssembly/_Imports.razor @@ -8,3 +8,4 @@ @using BlazorSample @using BlazorSample.Shared @using BlazorSample.Shared.templated_components +@using BlazorSample.Shared.event_handing diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample1.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample1.razor new file mode 100644 index 000000000000..16edc1913d5e --- /dev/null +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample1.razor @@ -0,0 +1,36 @@ +@page "/event-handler-example-1" + +

@currentHeading

+ +

+ + +

+ +

+ +

+ +@code { + private string currentHeading = "Initial heading"; + private string newHeading; + private string checkedMessage = "Not changed yet"; + + private void UpdateHeading() + { + currentHeading = $"{newHeading}!!!"; + } + + private void CheckChanged() + { + checkedMessage = $"Last changed at {DateTime.Now}"; + } +} diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample2.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample2.razor new file mode 100644 index 000000000000..b4a920afe315 --- /dev/null +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample2.razor @@ -0,0 +1,25 @@ +@page "/event-handler-example-2" + +

@currentHeading

+ +

+ + +

+ +@code { + private string currentHeading = "Initial heading"; + private string newHeading; + + private async Task UpdateHeading() + { + await Task.Delay(2000); + + currentHeading = $"{newHeading}!!!"; + } +} diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample3.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample3.razor new file mode 100644 index 000000000000..5e6722a08f21 --- /dev/null +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample3.razor @@ -0,0 +1,21 @@ +@page "/event-handler-example-3" + +@for (var i = 0; i < 4; i++) +{ +

+ +

+} + +

@mousePointerMessage

+ +@code { + private string mousePointerMessage; + + private void ReportPointerLocation(MouseEventArgs e) + { + mousePointerMessage = $"Mouse coordinates: {e.ScreenX}:{e.ScreenY}"; + } +} diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample4.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample4.razor new file mode 100644 index 000000000000..8f934b888858 --- /dev/null +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample4.razor @@ -0,0 +1,13 @@ +@page "/event-handler-example-4" + +

@heading

+ +

+ +

+ +@code { + private string heading = "Initial heading"; +} diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample5.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample5.razor new file mode 100644 index 000000000000..142713395074 --- /dev/null +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample5.razor @@ -0,0 +1,23 @@ +@page "/event-handler-example-5" + +

@heading

+ +@for (var i = 1; i < 4; i++) +{ + var buttonNumber = i; + +

+ +

+} + +@code { + private string heading = "Select a button to learn its position"; + + private void UpdateHeading(MouseEventArgs e, int buttonNumber) + { + heading = $"Selected #{buttonNumber} at {e.ClientX}:{e.ClientY}"; + } +} diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample6.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample6.razor new file mode 100644 index 000000000000..f3ebbf3a2e5b --- /dev/null +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample6.razor @@ -0,0 +1,17 @@ +@page "/event-handler-example-6" + +

+ +

+ +@code { + private int count = 0; + + private void KeyHandler(KeyboardEventArgs e) + { + if (e.Key == "+") + { + count++; + } + } +} diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample7.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample7.razor new file mode 100644 index 000000000000..3a2f10db199c --- /dev/null +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample7.razor @@ -0,0 +1,34 @@ +@page "/event-handler-example-7" + + + +
+

Parent div

+ +
+ Child div that doesn't stop propagation when selected. +
+ +
+ Child div that stops propagation when selected. +
+
+ +

+ @message +

+ +@code { + private bool stopPropagation = false; + private string message; + + private void OnSelectParentDiv() => + message = $"The parent div was selected. {DateTime.Now}"; + + private void OnSelectChildDiv() => + message = $"A child div was selected. {DateTime.Now}"; +} diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample8.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample8.razor new file mode 100644 index 000000000000..5cdd12a376f7 --- /dev/null +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/EventHandlerExample8.razor @@ -0,0 +1,18 @@ +@page "/event-handler-example-8" + +

+ +

+ + + +@code { + private ElementReference exampleInput; + + private async Task ChangeFocus() + { + await exampleInput.FocusAsync(); + } +} diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/Parent.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/Parent.razor new file mode 100644 index 000000000000..896aa0cb950d --- /dev/null +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Pages/event-handling/Parent.razor @@ -0,0 +1,18 @@ +@page "/parent" + +

Parent-child example

+ + + Content of the child component is supplied by the parent component. + + +

@message

+ +@code { + private string message; + + private void ShowMessage(MouseEventArgs e) + { + message = $"Blaze a new trail with Blazor! ({e.ScreenX}:{e.ScreenY})"; + } +} diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Shared/event-handling/Child.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Shared/event-handling/Child.razor new file mode 100644 index 000000000000..2cd0eaed242d --- /dev/null +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/Shared/event-handling/Child.razor @@ -0,0 +1,16 @@ +

+ +

+ +@code { + [Parameter] + public string Title { get; set; } + + [Parameter] + public RenderFragment ChildContent { get; set; } + + [Parameter] + public EventCallback OnClickCallback { get; set; } +} diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/_Imports.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/_Imports.razor index 056a7e23e9bb..be5caaf1a769 100644 --- a/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/_Imports.razor +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_Server/_Imports.razor @@ -9,3 +9,4 @@ @using BlazorSample @using BlazorSample.Shared @using BlazorSample.Shared.templated_components +@using BlazorSample.Shared.event_handing diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample1.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample1.razor new file mode 100644 index 000000000000..16edc1913d5e --- /dev/null +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample1.razor @@ -0,0 +1,36 @@ +@page "/event-handler-example-1" + +

@currentHeading

+ +

+ + +

+ +

+ +

+ +@code { + private string currentHeading = "Initial heading"; + private string newHeading; + private string checkedMessage = "Not changed yet"; + + private void UpdateHeading() + { + currentHeading = $"{newHeading}!!!"; + } + + private void CheckChanged() + { + checkedMessage = $"Last changed at {DateTime.Now}"; + } +} diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample2.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample2.razor new file mode 100644 index 000000000000..b4a920afe315 --- /dev/null +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample2.razor @@ -0,0 +1,25 @@ +@page "/event-handler-example-2" + +

@currentHeading

+ +

+ + +

+ +@code { + private string currentHeading = "Initial heading"; + private string newHeading; + + private async Task UpdateHeading() + { + await Task.Delay(2000); + + currentHeading = $"{newHeading}!!!"; + } +} diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample3.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample3.razor new file mode 100644 index 000000000000..5e6722a08f21 --- /dev/null +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample3.razor @@ -0,0 +1,21 @@ +@page "/event-handler-example-3" + +@for (var i = 0; i < 4; i++) +{ +

+ +

+} + +

@mousePointerMessage

+ +@code { + private string mousePointerMessage; + + private void ReportPointerLocation(MouseEventArgs e) + { + mousePointerMessage = $"Mouse coordinates: {e.ScreenX}:{e.ScreenY}"; + } +} diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample4.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample4.razor new file mode 100644 index 000000000000..8f934b888858 --- /dev/null +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample4.razor @@ -0,0 +1,13 @@ +@page "/event-handler-example-4" + +

@heading

+ +

+ +

+ +@code { + private string heading = "Initial heading"; +} diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample5.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample5.razor new file mode 100644 index 000000000000..142713395074 --- /dev/null +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample5.razor @@ -0,0 +1,23 @@ +@page "/event-handler-example-5" + +

@heading

+ +@for (var i = 1; i < 4; i++) +{ + var buttonNumber = i; + +

+ +

+} + +@code { + private string heading = "Select a button to learn its position"; + + private void UpdateHeading(MouseEventArgs e, int buttonNumber) + { + heading = $"Selected #{buttonNumber} at {e.ClientX}:{e.ClientY}"; + } +} diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample6.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample6.razor new file mode 100644 index 000000000000..f3ebbf3a2e5b --- /dev/null +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample6.razor @@ -0,0 +1,17 @@ +@page "/event-handler-example-6" + +

+ +

+ +@code { + private int count = 0; + + private void KeyHandler(KeyboardEventArgs e) + { + if (e.Key == "+") + { + count++; + } + } +} diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample7.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample7.razor new file mode 100644 index 000000000000..3a2f10db199c --- /dev/null +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample7.razor @@ -0,0 +1,34 @@ +@page "/event-handler-example-7" + + + +
+

Parent div

+ +
+ Child div that doesn't stop propagation when selected. +
+ +
+ Child div that stops propagation when selected. +
+
+ +

+ @message +

+ +@code { + private bool stopPropagation = false; + private string message; + + private void OnSelectParentDiv() => + message = $"The parent div was selected. {DateTime.Now}"; + + private void OnSelectChildDiv() => + message = $"A child div was selected. {DateTime.Now}"; +} diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample8.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample8.razor new file mode 100644 index 000000000000..5cdd12a376f7 --- /dev/null +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample8.razor @@ -0,0 +1,18 @@ +@page "/event-handler-example-8" + +

+ +

+ + + +@code { + private ElementReference exampleInput; + + private async Task ChangeFocus() + { + await exampleInput.FocusAsync(); + } +} diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/Parent.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/Parent.razor new file mode 100644 index 000000000000..896aa0cb950d --- /dev/null +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/Parent.razor @@ -0,0 +1,18 @@ +@page "/parent" + +

Parent-child example

+ + + Content of the child component is supplied by the parent component. + + +

@message

+ +@code { + private string message; + + private void ShowMessage(MouseEventArgs e) + { + message = $"Blaze a new trail with Blazor! ({e.ScreenX}:{e.ScreenY})"; + } +} diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Shared/event-handling/Child.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Shared/event-handling/Child.razor new file mode 100644 index 000000000000..2cd0eaed242d --- /dev/null +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/Shared/event-handling/Child.razor @@ -0,0 +1,16 @@ +

+ +

+ +@code { + [Parameter] + public string Title { get; set; } + + [Parameter] + public RenderFragment ChildContent { get; set; } + + [Parameter] + public EventCallback OnClickCallback { get; set; } +} diff --git a/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/_Imports.razor b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/_Imports.razor index 96b39e0d9091..4f731bbeaaae 100644 --- a/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/_Imports.razor +++ b/aspnetcore/blazor/common/samples/5.x/BlazorSample_WebAssembly/_Imports.razor @@ -9,3 +9,4 @@ @using BlazorSample @using BlazorSample.Shared @using BlazorSample.Shared.templated_components +@using BlazorSample.Shared.event_handing diff --git a/aspnetcore/blazor/components/event-handling.md b/aspnetcore/blazor/components/event-handling.md index 157455fb452b..bae730b7b1fb 100644 --- a/aspnetcore/blazor/components/event-handling.md +++ b/aspnetcore/blazor/components/event-handling.md @@ -5,7 +5,7 @@ description: Learn about Blazor's event handling features, including event argum monikerRange: '>= aspnetcore-3.1' ms.author: riande ms.custom: mvc -ms.date: 03/15/2021 +ms.date: 03/16/2021 no-loc: [appsettings.json, "ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR] uid: blazor/components/event-handling --- @@ -13,10 +13,10 @@ uid: blazor/components/event-handling 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 [Document Object Model (DOM) event](https://developer.mozilla.org/docs/Web/Events) (for example, `onclick`). -* The `{DELEGATE}` placeholder is the C# delegate handler. +* The `{DOM EVENT}` placeholder is a [Document Object Model (DOM) event](https://developer.mozilla.org/docs/Web/Events) (for example, `click`). +* The `{DELEGATE}` placeholder is the C# delegate event handler. -Event handling characteristics include the following: +For event handling: * 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). @@ -29,44 +29,17 @@ The following code: `Pages/EventHandlerExample1.razor`: -```razor -@page "/event-handler-example-1" - -

@currentHeading

+::: moniker range=">= aspnetcore-5.0" -

- - -

+[!code-razor[](~/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample1.razor?highlight=10,17,27-30,32-35)] -

- -

+::: moniker-end -@code { - private string currentHeading = "Initial heading"; - private string newHeading; - private string checkedMessage = "Not changed yet"; +::: moniker range="< aspnetcore-5.0" - private void UpdateHeading() - { - currentHeading = $"{newHeading}!!!"; - } +[!code-razor[](~/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample1.razor?highlight=10,17,27-30,32-35)] - private void CheckChanged() - { - checkedMessage = $"Last changed at {DateTime.Now}"; - } -} -``` +::: moniker-end In the following example, `UpdateHeading`: @@ -75,33 +48,17 @@ In the following example, `UpdateHeading`: `Pages/EventHandlerExample2.razor`: -```razor -@page "/event-handler-example-2" +::: moniker range=">= aspnetcore-5.0" -

@currentHeading

+[!code-razor[](~/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample2.razor?highlight=10,19-24)] -

- - -

+::: moniker-end -@code { - private string currentHeading = "Initial heading"; - private string newHeading; +::: moniker range="< aspnetcore-5.0" - private async Task UpdateHeading() - { - await Task.Delay(2000); +[!code-razor[](~/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample2.razor?highlight=10,19-24)] - currentHeading = $"{newHeading}!!!"; - } -} -``` +::: moniker-end ## Event arguments @@ -115,29 +72,17 @@ For events that support an event argument type, specifying an event parameter in `Pages/EventHandlerExample3.razor`: -```razor -@page "/event-handler-example-3" +::: moniker range=">= aspnetcore-5.0" -@for (var i = 0; i < 4; i++) -{ -

- -

-} +[!code-razor[](~/blazor/common/samples/5.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample3.razor?highlight=17-20)] -

@mousePointerMessage

+::: moniker-end -@code { - private string mousePointerMessage; +::: moniker range="< aspnetcore-5.0" - private void ReportPointerLocation(MouseEventArgs e) - { - mousePointerMessage = $"Mouse coordinates: {e.ScreenX}:{e.ScreenY}"; - } -} -``` +[!code-razor[](~/blazor/common/samples/3.x/BlazorSample_WebAssembly/Pages/event-handling/EventHandlerExample3.razor?highlight=17-20)] + +::: moniker-end Supported are shown in the following table. @@ -208,7 +153,7 @@ Custom events with custom event arguments are generally enabled with the followi } ``` -1. Register the custom event with the preceding handler in `wwwroot/index.html` (Blazor WebAssembly) or `Pages/_Host.cshtml` (Blazor Server) immediately after the Blazor script: +1. Register the custom event with the preceding handler in `wwwroot/index.html` (Blazor WebAssembly) or `Pages/_Host.cshtml` (Blazor Server) immediately after the Blazor `