Skip to content

Commit 81ca525

Browse files
authored
Event binding guidance improvements (#36611)
1 parent 0c62ae9 commit 81ca525

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

aspnetcore/blazor/components/data-binding.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ As a demonstration of how data binding composes in HTML, the following example b
107107

108108
When the `BindTheory` component is rendered, the `value` of the HTML demonstration `<input>` element comes from the `InputValue` property. When the user enters a value in the text box and changes element focus, the `onchange` event is fired and the `InputValue` property is set to the changed value. In reality, code execution is more complex because [`@bind`](xref:mvc/views/razor#bind) handles cases where type conversions are performed. In general, [`@bind`](xref:mvc/views/razor#bind) associates the current value of an expression with the `value` attribute of the `<input>` and handles changes using the registered handler.
109109

110-
Bind a property or field on other DOM events by including an `@bind:event="{EVENT}"` attribute with a DOM event for the `{EVENT}` placeholder. The following example binds the `InputValue` property to the `<input>` element's value when the element's `oninput` event ([`input`](https://developer.mozilla.org/docs/Web/API/HTMLElement/input_event)) is triggered. Unlike the `onchange` event ([`change`](https://developer.mozilla.org/docs/Web/API/HTMLElement/change_event)), which fires when the element loses focus, `oninput` ([`input`](https://developer.mozilla.org/docs/Web/API/HTMLElement/input_event)) fires when the value of the text box changes.
110+
Default binding with `@bind` uses the [`change`](https://developer.mozilla.org/docs/Web/API/HTMLElement/change_event) DOM event (`onchange`). Binding can also take place on the [`input`](https://developer.mozilla.org/docs/Web/API/HTMLElement/input_event) DOM event using the `@bind:event="{EVENT}"` attribute, where the `{EVENT}` placeholder is either `oninput` or `onchange` (default).
111+
112+
The following example binds the `InputValue` property to the `<input>` element's value when the element's `oninput` event is triggered. Unlike the `onchange` event, which fires when the element loses focus, `oninput` fires when the value of the text box changes.
111113

112114
`Page/BindEvent.razor`:
113115

@@ -147,6 +149,21 @@ Bind a property or field on other DOM events by including an `@bind:event="{EVEN
147149

148150
:::moniker-end
149151

152+
:::moniker range=">= aspnetcore-6.0"
153+
154+
To bind on other DOM events, use either of the following approaches:
155+
156+
* [Delegate event handlers](xref:blazor/components/event-handling#delegate-event-handlers)
157+
* [Custom event arguments](xref:blazor/components/event-handling#custom-event-arguments)
158+
159+
:::moniker-end
160+
161+
:::moniker range="< aspnetcore-6.0"
162+
163+
To bind on other DOM events, use [delegate event handlers](xref:blazor/components/event-handling#delegate-event-handlers).
164+
165+
:::moniker-end
166+
150167
:::moniker range="< aspnetcore-7.0"
151168

152169
Razor attribute binding is case-sensitive:

aspnetcore/blazor/components/event-handling.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Specify delegate event handlers in Razor component markup with [`@on{DOM EVENT}=
2121
* The `{DOM EVENT}` placeholder is a [DOM event](https://developer.mozilla.org/docs/Web/Events) (for example, `click`).
2222
* The `{DELEGATE}` placeholder is the C# delegate event handler.
2323

24+
For supported events, see <xref:Microsoft.AspNetCore.Components.Web.EventHandlers>.
25+
2426
For event handling:
2527

2628
:::moniker range=">= aspnetcore-8.0"

0 commit comments

Comments
 (0)