dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.3k stars 9.96k forks source link

[Blazor] preventDefault does not function in Auto render mode, even if the page is marked with InteractiveWebAssembly #52514

Open octavian-cacina-gotomaxx opened 10 months ago

octavian-cacina-gotomaxx commented 10 months ago

Is there an existing issue for this?

Describe the bug

<a href="#" @onclick="IncrementCount" @onclick:preventDefault>Click me</a>

it is not preventing the default handling in a Blazor App with Auto interactivity, even if the page is properly configured for WebAssembly. If the App is created only with WebAssembly interactivity, it works as expected.

50992 is similar, but it does not indicate any resolution.

Expected Behavior

preventDefault should prevent any further navigation

Steps To Reproduce

Exceptions (if any)

If the Blazor App is created only with WebAssembly interactivity, it works as expected.

.NET Version

8.0.100

Anything else?

No response

surayya-MS commented 10 months ago

Thanks for contacting us! Could you please try removing href="#" and @onclick:preventDefault <a @onclick="IncrementCount">Click me</a> and se if it works?

ghost commented 10 months ago

Thanks for contacting us.

We're moving this issue to the .NET 9 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

octavian-cacina-gotomaxx commented 10 months ago

Could you please try removing href="#" and @onclick:preventDefault <a @onclick="IncrementCount">Click me</a> and se if it works?

I have tried, but then the element is not clickable, and the handler ist not called.

cssack commented 9 months ago

I encountered a similar issue.

My objective was to create an anchor tag with an href attribute, allowing users to right-click and open the link in a new tab. Concurrently, I aimed to implement some sophisticated logic upon clicking. To achieve this, I needed the capability to prevent the default action while simultaneously detecting a click event in C#.

Ultimately, I resolved the issue as follows:

r.OpenElement(0, "a");
r.AddAttribute(1, "href", _navigation.Url);
r.AddAttribute(2, "onclick", EventCallback.Factory.Create<MouseEventArgs>(this, OnClicked));
r.AddAttribute(3, "onclick", "return false;");
...

This solution effectively accomplished the following:

I used ChatGPT to improve the readability of this comment

ghost commented 9 months ago

Thanks for contacting us.

We're moving this issue to the .NET 9 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

Ciantic commented 2 weeks ago

I have @rendermode InteractiveServer and it's not working with it either.