Monday, May 20, 2024
155
rated 0 times [  158] [ 3]  / answers: 1 / hits: 6477  / 4 Years ago, tue, march 17, 2020, 12:00:00

I am working on an ASP.NET Core 3.1 Blazor Server-Side application that has a Blazor component with an EditForm. I want to warn the user that they have unsaved data if they try to navigate off of the page (i.e. click the browser back button or make a Nav menu selection that loads another page within the same application).



I have read posts about using the javascript window.onbeforeunload but I was not sure if invoking javascript on a Blazor server-side would mess up the SignalR connection.



I have also read posts about using a CircuitHandler but I wan't sure if the SignalR circuit changed if the user just moves to a different page in the same application.



Any recommendations on how to best handle this requirement?


More From » asp.net-core

 Answers
2

I'm guessing it's intentional to only do this for programmatic navigation, since it wouldn't be possible to block navigation that occurs when people click on tags. For Blazor Server, the logic has to run asynchronously so the JS event can't be cancelled. Event for Blazor WebAssembly or any JS-based SPA framework, it doesn't make sense to block clicks on tags, since browsers won't always honour your intentions (e.g., if a user right-clicks and chooses Open in new tab).




SteveSandersonMS



See also this and this



As you can see, not much can be done in this respect. However, you should design your pages in a way that can give you as much control over the users' actions as possible, including the good idea to save data entered in the forms in the local storage, and retrieve it in such cases as the user exit your app abruptly, etc.



Note: You may use JSInterop with SignalR. No problem.



Note: A CircuitHandler handles the life cycle of a websocket connection, and may only partially suits your requirements.


[#4462] Saturday, March 14, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lara

Total Points: 462
Total Questions: 100
Total Answers: 102

Location: Jersey
Member since Mon, Jun 14, 2021
3 Years ago
lara questions
Thu, May 19, 22, 00:00, 2 Years ago
Thu, Apr 1, 21, 00:00, 3 Years ago
Mon, Jul 27, 20, 00:00, 4 Years ago
Wed, Feb 12, 20, 00:00, 4 Years ago
;