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
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -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/includes/2-attach-csharp-code-dom-events-blazor-event-handlers.md
+5-5Lines changed: 5 additions & 5 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
@@ -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:
Copy file name to clipboardExpand all lines: learn-pr/aspnetcore/blazor-improve-how-forms-work/includes/4-take-advantage-power-blazor-forms.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -85,7 +85,7 @@ The following image shows an example of the page running:
85
85
86
86
The HTML `<form>` element supports the `<input>` element to enable the user to enter data. The `<input>` has a `type` property that specifies the type of the input and how it should be displayed; as a number, a text box, a radio button, a check box, a button, and so on.
87
87
88
-
Blazor has its own set of components designed to work specifically with the `<EditForm>` element and support data binding among other features. The following table lists these components. When Blazor renders a page containing these components, they are converted to the corresponding HTML `<input>` elements listed in the table. Some of the Blazor components are generic; the type parameter is ascertained by the Blazor runtime depending on the type of the data bound to the element:
88
+
Blazor has its own set of components designed to work specifically with the `<EditForm>` element and support data binding among other features. The following table lists these components. When Blazor renders a page containing these components, they're converted to the corresponding HTML `<input>` elements listed in the table. Some of the Blazor components are generic; the Blazor runtime ascertains the type paramater depending on the type of the data bound to the element:
@@ -180,12 +180,12 @@ When you run the form, it looks like this:
180
180
181
181
## Handle form submission
182
182
183
-
You've seen that you can use an `EditForm` to modify data in the underlying model. When the changes are complete, you can submit the form to validate the data on the server and save the changes. Blazor supports two types of validation; declarative and programmatic. Declarative validation rules operate on the client, in the browser. They're useful for performing basic client-side validation before data is transmitted to the server. Server-side validation is useful for handling complex scenarios that are not available with declarative validation, such as cross-checking the data in a field against data from other sources. A real-world application should utilize a combination of client-side and server-side validation. The client-side validation traps basic user input errors and prevents many cases of invalid data being sent to the server for processing. Server-side validation ensures that a user request to save data doesn't attempt to bypass data validation and store incomplete or corrupt data.
183
+
You've seen that you can use an `EditForm` to modify data in the underlying model. When the changes are complete, you can submit the form to validate the data on the server and save the changes. Blazor supports two types of validation; declarative and programmatic. Declarative validation rules operate on the client, in the browser. They're useful for performing basic client-side validation before data is transmitted to the server. Server-side validation is useful for handling complex scenarios that aren't available with declarative validation, such as cross-checking the data in a field against data from other sources. A real-world application should utilize a combination of client-side and server-side validation. The client-side validation traps basic user input errors and prevents many cases of invalid data being sent to the server for processing. Server-side validation ensures that a user request to save data doesn't attempt to bypass data validation and store incomplete or corrupt data.
184
184
185
185
> [!NOTE]
186
-
> You can also trap JavaScript events such as `onchange` and `oninput`, and the Blazor equivalent `@onchange` and `@oninput` events for many controls in an `EditForm`. You can use these events to examine and validate data programmatically, on a field-by-field basis, before the user submits the form. However, this approach is not recommended. It can be frustrating to a user to have validation messages appear as they enter each keystroke or tab between fields. Save validation for when the user has completed their input.
186
+
> You can also trap JavaScript events such as `onchange` and `oninput`, and the Blazor equivalent `@onchange` and `@oninput` events for many controls in an `EditForm`. You can use these events to examine and validate data programmatically, on a field-by-field basis, before the user submits the form. However, this approach isn't recommended. It can be frustrating to a user to have validation messages appear as they enter each keystroke or tab between fields. Save validation for when the user has completed their input.
187
187
188
-
An `EditForm` has three events that run when it is submitted:
188
+
An `EditForm` has three events that run when it's submitted:
189
189
190
190
-`OnValidSubmit`: This event is triggered if the input fields successfully pass the validation rules defined by their validation attributes.
191
191
-`OnInvalidSubmit`: This event is triggered if any of the input fields on the form fail the validation defined by their validation attributes.
@@ -204,8 +204,8 @@ The `EditForm` tracks the state of the current object acting as model, including
204
204
205
205
The following example shows the `EditForm` from the previous example with a submit button. The `EditForm` captures the `OnSubmit` event to validate the changes made to a T-shirt object. In this example, only certain combinations of values are allowed:
206
206
207
-
- Red T-shirts are not available in the Extra Large size.
208
-
- Blue T-shirts are not available in the Small or Medium sizes.
207
+
- Red T-shirts aren't available in the Extra Large size.
208
+
- Blue T-shirts aren't available in the Small or Medium sizes.
209
209
- White T-shirts have a maximum price of $50.
210
210
211
211
If an illegal combination is detected, the `Message` field on the form displays the reason for the validation failure. If the fields are valid, the data is processed and the data is saved (the logic for this process isn't shown).
@@ -225,7 +225,7 @@ If an illegal combination is detected, the `Message` field on the form displays
Copy file name to clipboardExpand all lines: learn-pr/aspnetcore/blazor-improve-how-forms-work/includes/5-exercise-create-address-form-blazor-components.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,12 +46,12 @@ In this exercise, you'll replace the current HTML fields with a Blazor component
46
46
1. In the file explorer, expand **Shared**, then select **AddressEditor.razor**.
47
47
48
48
1. Select the **Edit** menu, then select **Replace**.
49
-
1. In the first field, enter `<input` in the replace field enter `<InputText`, then select replace all.
49
+
1. In the first field enter `<input`, in the replace field enter `<InputText`, and then select replace all.
50
50
51
51
:::image type="content" source="../media/5-replace-input-elements.png" alt-text="Screenshot of Visual Studio Code and the text replace dialog.":::
52
52
53
53
1. Select the **Edit** menu, then select **Replace**.
54
-
1. In the first field, enter `@bind=` in the replace field enter `@bind-Value=`, then select replace all.
54
+
1. In the first field enter `@bind=`, in the replace field enter `@bind-Value=`, and then select replace all.
55
55
1. Remove the `@ref="startName"` code on the Name field.
56
56
1. Remove all the code below the Parameter declaration in the `@code` block. The block should now look like this.
Copy file name to clipboardExpand all lines: learn-pr/aspnetcore/blazor-improve-how-forms-work/includes/7-exercise-add-server-client-side-data-validation-address-form.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -67,7 +67,7 @@ In this exercise, you'll replace the current server-side validation to use data
67
67
68
68
:::image type="content" source="../media/7-show-validation-errors.png" alt-text="Screenshot of the error messages for each field.":::
69
69
70
-
This interaction improves the error checks for each field, but the error for each field would be much better next to the field it's related to.
70
+
This interaction improves the error checks for each field, but the error for each field would be better next to the field it's related to.
71
71
72
72
1. Press <kbd>Shift</kbd> + <kbd>F5</kbd> to stop the app from running.
0 commit comments