Skip to content

Commit 029c12c

Browse files
author
KB Bot
committed
Added new kb article form-prompt-unsaved-changes
1 parent 2c7ed74 commit 029c12c

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
---
2+
title: How to Prompt Users for Unsaved Changes on Navigation
3+
description: A guide on how to implement a warning for unsaved changes when attempting to navigate away from a TelerikForm in Blazor applications.
4+
type: how-to
5+
page_title: How to Prompt Users for Unsaved Changes in Blazor TelerikForm
6+
slug: form-kb-prompt-unsaved-changes
7+
tags: blazor, form, navigation, unsaved changes
8+
res_type: kb
9+
ticketid: 1682122, 1629951
10+
---
11+
12+
## Environment
13+
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>Form for Blazor</td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
## Description
24+
25+
This knowledge base article answers the following questions:
26+
27+
- How to detect unsaved changes in a Blazor TelerikForm?
28+
- How to prompt users before navigating away from unsaved changes in Form?
29+
- How to conditionally navigate away from a form?
30+
31+
## Solution
32+
33+
To prompt the users with a warning message when they attempt to navigate away from a page with unsaved changes in a TelerikForm, follow these steps:
34+
35+
1. Inject [`NavigationManager`](https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/routing?view=aspnetcore-7.0#handleprevent-location-changes) to handle navigation
36+
2. Register a handler using `NavigationManager.RegisterLocationChangingHandler()` to intercept navigation attempts
37+
3. In [`LocationChangingHandler`](https://www.telerik.com/blogs/blazor-new-locationchanging-events-dotnet-7), check if the form has unsaved changes
38+
4. Add a [`<TelerikDialog>`](slug:dialog-overview) to prompt the user when there are unsaved changes
39+
5. Implement a `PreventLeaving()` method and `ProceedNavigation()` method to close the dialog without navigating and to manually navigate to the stored URL if the user confirms
40+
41+
`````RAZOR
42+
@implements IDisposable
43+
@inject NavigationManager NavigationManager
44+
45+
<TelerikForm @ref="@FormRef"
46+
Model="@Employee"
47+
Width="300px">
48+
<FormItems>
49+
<FormItem Field="@nameof(Person.Id)" Enabled="false"></FormItem>
50+
<FormAutoGeneratedItems />
51+
</FormItems>
52+
</TelerikForm>
53+
54+
<NavLink href="test">Go To Other Page</NavLink>
55+
56+
<TelerikDialog @bind-Visible="@ShowNavigationDialog"
57+
Title="Confirm Navigation">
58+
<DialogContent>
59+
You have unsaved changes. Are you sure you want to leave this page?
60+
</DialogContent>
61+
<DialogButtons>
62+
<TelerikButton OnClick="@PreventLeaving">No</TelerikButton>
63+
<TelerikButton ThemeColor="primary" OnClick="@ProceedNavigation">Yes</TelerikButton>
64+
</DialogButtons>
65+
</TelerikDialog>
66+
67+
@code {
68+
private Person Employee = new Person();
69+
private TelerikForm? FormRef { get; set; }
70+
private IDisposable? NavEventRegistration;
71+
private bool ShowNavigationDialog = false;
72+
private string? NextUrl;
73+
private bool isNavigationConfirmed = false; // Flag to track navigation confirmation
74+
75+
private ValueTask LocationChangingHandler(LocationChangingContext args)
76+
{
77+
// Prevent the confirmation dialog from appearing again once the user confirmed navigation
78+
if (isNavigationConfirmed)
79+
{
80+
return ValueTask.CompletedTask;
81+
}
82+
83+
if (FormRef?.EditContext.IsModified() ?? false)
84+
{
85+
// Prevent navigation and store the target URL
86+
args.PreventNavigation();
87+
NextUrl = args.TargetLocation;
88+
ShowNavigationDialog = true;
89+
90+
// Force Blazor to re-render
91+
InvokeAsync(StateHasChanged);
92+
}
93+
94+
return ValueTask.CompletedTask;
95+
}
96+
97+
private void PreventLeaving()
98+
{
99+
// Simply close the dialog without changing the page
100+
ShowNavigationDialog = false;
101+
StateHasChanged();
102+
}
103+
104+
private void ProceedNavigation()
105+
{
106+
// Set the flag to indicate navigation is confirmed
107+
isNavigationConfirmed = true;
108+
109+
// Navigate manually to the stored target URL
110+
if (!string.IsNullOrEmpty(NextUrl))
111+
{
112+
NavigationManager.NavigateTo(NextUrl);
113+
}
114+
115+
// Close the dialog after confirming navigation
116+
ShowNavigationDialog = false;
117+
}
118+
119+
protected override void OnInitialized()
120+
{
121+
Employee = new Person()
122+
{
123+
Id = 1,
124+
FirstName = "John",
125+
LastName = "Doe",
126+
BirthDate = DateTime.Today.AddYears(-30)
127+
};
128+
129+
// Register the navigation handler
130+
NavEventRegistration = NavigationManager.RegisterLocationChangingHandler(LocationChangingHandler);
131+
}
132+
133+
public void Dispose()
134+
{
135+
NavEventRegistration?.Dispose();
136+
}
137+
138+
public class Person
139+
{
140+
public int Id { get; set; }
141+
public string FirstName { get; set; } = string.Empty;
142+
public string LastName { get; set; } = string.Empty;
143+
public DateTime BirthDate { get; set; } = DateTime.Today;
144+
}
145+
}
146+
`````
147+
148+
## See Also
149+
150+
- [Blazor Form Overview](slug:form-overview)
151+
- [Blazor Dialog Overview](slug:dialog-overview)

0 commit comments

Comments
 (0)