You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: learn-pr/aspnetcore/blazor-improve-how-forms-work/2-attach-csharp-code-dom-events-blazor-event-handlers.yml
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ metadata:
5
5
unitType: learning-content
6
6
title: "Attach C# code to DOM events with Blazor event handlers"
7
7
description: "Create handlers to process DOM events using C#"
8
-
ms.date: 11/09/2023
8
+
ms.date: 01/09/2025
9
9
author: csharpfritz
10
10
ms.author: jefritz
11
11
ms.manager: markl
@@ -20,7 +20,7 @@ quiz:
20
20
choices:
21
21
- content: "C# events"
22
22
isCorrect: false
23
-
explanation: "C# events do not properly transfer between components"
23
+
explanation: "C# events don't properly transfer between components"
24
24
- content: "EventCallback"
25
25
isCorrect: true
26
26
explanation: "EventCallbacks look like parameters on a component and can be specified on a Blazor page or another component"
@@ -34,7 +34,7 @@ quiz:
34
34
explanation: "preventDefault prevents the default action in the DOM element from executing"
35
35
- content: "return false"
36
36
isCorrect: false
37
-
explanation: "return false was a technique to stop an event in other JavaScript frameworks, but does not prevent the default action"
37
+
explanation: "return false was a technique to stop an event in other JavaScript frameworks, but doesn't prevent the default action"
38
38
- content: "throw new Exception()"
39
39
isCorrect: false
40
40
explanation: "Throwing an exception might stop the default action from executing, but it also raises an error that you might not want your application users to interact with"
Copy file name to clipboardExpand all lines: learn-pr/aspnetcore/blazor-improve-how-forms-work/3-exercise-create-blazor-event-handler-onclick-events.yml
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ metadata:
5
5
unitType: Exercise
6
6
title: "Exercise - Create a Blazor event handler for onclick events"
7
7
description: "Handle the onclick event for an HTML form control using C#"
Copy file name to clipboardExpand all lines: learn-pr/aspnetcore/blazor-improve-how-forms-work/7-exercise-add-server-client-side-data-validation-address-form.yml
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ metadata:
5
5
unitType: Exercise
6
6
title: "Exercise - Add server-side and client-side data validation to the address form "
7
7
description: "Use data annotations on a model to add custom validation to a Blazor EditForm component."
Copy file name to clipboardExpand all lines: learn-pr/aspnetcore/blazor-improve-how-forms-work/includes/2-attach-csharp-code-dom-events-blazor-event-handlers.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ In this unit, you'll look in detail at the third option; how to create a Blazor
8
8
9
9
## Handle an event with Blazor and C#
10
10
11
-
Each element in the HTML markup of a Blazor app supports many events. Most of these events correspond to the DOM events available in regular web applications, but you can also create user-defined events that are triggered by writing code. To capture an event with Blazor, you write a C# method that handles the event, then bind the event to the method with a Blazor directive. For a DOM event, the Blazor directive shares the same name as the equivalent HTML event, such as `@onkeydown` or `@onfocus`. For example, the sample app generated by using the Blazor Server App contains the following code on the **Counter.razor** page. This page displays a button. When the user selects the button, the `@onclick` event triggers the `IncrementCount` method that increments a counter indicating how many times the button has been clicked. The value of the counter variable is displayed by the \<p\> element on the page:
11
+
Each element in the HTML markup of a Blazor app supports many events. Most of these events correspond to the DOM events available in regular web applications, but you can also create user-defined events that are triggered by writing code. To capture an event with Blazor, you write a C# method that handles the event, then bind the event to the method with a Blazor directive. For a DOM event, the Blazor directive shares the same name as the equivalent HTML event, such as `@onkeydown` or `@onfocus`. For example, the sample app generated by using the Blazor Server App contains the following code on the **Counter.razor** page. This page displays a button. When the user selects the button, the `@onclick` event triggers the `IncrementCount` method that increments a counter indicating how many times the button has been clicked. The \<p\> element on the page displays the value of the counter variable:
12
12
13
13
```razor
14
14
@page "/counter"
@@ -84,7 +84,7 @@ A traditional web application uses JavaScript to capture and process events. You
84
84
85
85
Besides the syntactic differences in the two versions of the event handler, you should note the following functional differences:
86
86
87
-
- JavaScript does not prefix the name of the event with an `@` sign; it's not a Blazor directive.
87
+
- JavaScript doesn't prefix the name of the event with an `@` sign; it's not a Blazor directive.
88
88
- In the Blazor code, you specify the name of the event-handling method when you attach it to an event. In JavaScript, you write a statement that calls the event-handling method; you specify round brackets and any parameters required.
89
89
- Most importantly, the JavaScript event handler runs in the browser, on the client. If you're building a Blazor Server App, the Blazor event handler runs on the server and only updates the browser with any changes made to the UI when the event handler completes. Additionally, the Blazor mechanism enables an event handler to access static data that's shared between sessions; the JavaScript model doesn't. However, handling some frequently occurring events such as `@onmousemove` can cause the user interface to become sluggish because they require a network round-trip to the server. You might prefer to handle events such as these in the browser, using JavaScript.
90
90
@@ -93,7 +93,7 @@ Besides the syntactic differences in the two versions of the event handler, you
93
93
94
94
## Handle events asynchronously
95
95
96
-
By default, Blazor event handlers are synchronous. If an event handler performs a potentially long-running operation, such as calling a web service, the thread on which the event handler runs will be blocked until the operation completes. This can lead to poor response in the user interface. To combat this, you can designate an event handler method as asynchronous. Use the C# `async` keyword. The method must return a `Task` object. You can then use the `await` operator inside the event handler method to initiate any long-running tasks on a separate thread and free the current thread for other work. When a long-running task completes, the event handler resumes. The example event handler below runs a time-consuming method asynchronously:
96
+
By default, Blazor event handlers are synchronous. If an event handler performs a potentially long-running operation, such as calling a web service, the thread on which the event handler runs will be blocked until the operation completes. This can lead to poor response in the user interface. To combat this, you can designate an event handler method as asynchronous. Use the C# `async` keyword. The method must return a `Task` object. You can then use the `await` operator inside the event handler method to initiate any long-running tasks on a separate thread and free the current thread for other work. When a long-running task completes, the event handler resumes. The example event handler that follows runs a time-consuming method asynchronously:
@@ -144,7 +144,7 @@ The following image shows the result when the user selects the button:
144
144
:::image type="content" source="../media/2-change-focus.png" alt-text="Screenshot of the web page after the user has clicked the button to set the focus to the input element.":::
145
145
146
146
> [!NOTE]
147
-
> An app should only direct the focus to a specific control for a specific reason, such as to ask the user to modify input after an error. Don't use focusing to force the user to navigate through the elements on a page in a fixed order; this can be very frustrating to the user who might want to revisit elements to change their input.
147
+
> An app should only direct the focus to a specific control for a specific reason, such as to ask the user to modify input after an error. Don't use focusing to force the user to navigate through the elements on a page in a fixed order. This can be frustrating to the user who might want to revisit elements to change their input.
148
148
149
149
## Write inline event handlers
150
150
@@ -167,7 +167,7 @@ C# supports lambda expressions. A lambda expression enables you to create an ano
167
167
> [!NOTE]
168
168
> For details on how lambda expressions work, read [Lambda expressions and anonymous functions](/dotnet/csharp/language-reference/operators/lambda-expressions).
169
169
170
-
This approach is also useful if you want to provide other arguments for an event-handling method. In the following example, the method `HandleClick` takes a `MouseEventArgs` parameter in the same way as an ordinary click event handler, but it also accepts a string parameter. The method processes the click event as before, but also displays the message in the user has pressed the <kbd>Ctrl</kbd> key. The lambda expression calls the `HandleCLick` method, passing in the `MouseEventArgs` parameter (`mouseEvent`), and a string.
170
+
This approach is also useful if you want to provide other arguments for an event-handling method. In the following example, the method `HandleClick` takes a `MouseEventArgs` parameter in the same way as an ordinary click event handler, but it also accepts a string parameter. The method processes the click event as before, but also displays the message if the user has pressed the <kbd>Ctrl</kbd> key. The lambda expression calls the `HandleCLick` method, passing in the `MouseEventArgs` parameter (`mouseEvent`), and a string.
171
171
172
172
```razor
173
173
@page "/counter"
@@ -202,7 +202,7 @@ This approach is also useful if you want to provide other arguments for an event
202
202
203
203
## Override default DOM actions for events
204
204
205
-
Several DOM events have default actions that run when the event occurs, regardless of whether there's an event handler available for that event. For example, the `@onkeypress` event for an \<input\> element always displays the character that corresponds to the key that the user has pressed, and handling the key press. In the next example, the `@onkeypress` event is used to convert the user's input to uppercase. Additionally, if the user types an `@` character, the event handler displays an alert:
205
+
Several DOM events have default actions that run when the event occurs, regardless of whether there's an event handler available for that event. For example, the `@onkeypress` event for an \<input\> element always displays the character that corresponds to the key that the user has pressed and handles the key press. In the next example, the `@onkeypress` event is used to convert the user's input to uppercase. Additionally, if the user types an `@` character, the event handler displays an alert:
@@ -279,7 +279,7 @@ A Blazor page can contain one or more Blazor components, and components can be n
279
279
280
280
A callback can take a single parameter. `EventCallback` is a generic type. The type parameter specifies the type of the argument passed to the callback.
281
281
282
-
As an example, consider the following scenario. You want to create a component named `TextDisplay` that enables the user to enter an input string and transform that string in some way; you might want to convert it to upper case, lower case, mixed case, filter characters from it, or perform some other type of transformation. However, when you write the code for the `TextDisplay` component, you don't know what the transformation process will be, and instead want to defer this operation to another component. The following code shows the `TextDisplay` component. It provides the input string in the form of an \<input\> element that enables the user to enter a text value.
282
+
As an example, consider the following scenario. You want to create a component named `TextDisplay` that enables the user to enter an input string and transform that string in some way. You might want to convert it to upper case, lower case, mixed case, filter characters from it, or perform some other type of transformation. However, when you write the code for the `TextDisplay` component, you don't know what the transformation process will be. Instead, you want to defer this operation to another component. The following code shows the `TextDisplay` component. It provides the input string in the form of an \<input\> element that enables the user to enter a text value.
283
283
284
284
```razor
285
285
@* TextDisplay component *@
@@ -318,7 +318,7 @@ namespace WebApplication.Data
318
318
319
319
The `key` field contains the value entered by the user, and the `TransformedKey` field will hold the transformed value of the key when it has been processed.
320
320
321
-
In this example, the `EventCallback` object is a component parameter, and its value is supplied when the component is created. This action is performed by another component, named `TextTransformer`:
321
+
In this example, the `EventCallback` object is a component parameter, and its value is supplied when the component is created. The component named `TextTransformer` performs this action:
0 commit comments