Skip to content

Razor Components - onclick handler not called if href is set in <a> tag #9773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MarkSFrancis opened this issue Apr 26, 2019 · 4 comments
Closed

Comments

@MarkSFrancis
Copy link

MarkSFrancis commented Apr 26, 2019

Describe the bug

Href attributes take precedence over click handlers in razor components for <a> tags

To Reproduce

Steps to reproduce the behavior:

  1. Create a new blazor app (server or client side - happens on both)
  2. Edit the Pages/Counter.razor file's button
    from
<button class="btn btn-primary" onclick="@IncrementCount">Click me</button>

to

<a href="#" class="btn btn-primary" onclick="@IncrementCount">Click me</a>
  1. Instead of incrementing the counter, it will now jump to the "#" url, navigating to the home page

Expected behavior

Increment the counter as it did before

Additional context

Installed dotnet version:
dotnet 3.0.100-preview4-011223

@enetstudio
Copy link

I believe this is the default behavior of opening a new page, and, since you've got no such url, the default behavior of the Blazor routing system is to navigate to the home page.

Try to return false from your click event.
Try either of these:
<a href="#" class="btn btn-primary" onclick="@IncrementCount;return false">Click me</a>

<a href="#" class="btn btn-primary" onclick="@(() => IncrementCount());return false">Click me</a>

Hope this works...

@MarkSFrancis
Copy link
Author

MarkSFrancis commented Apr 26, 2019

Unfortunately, it fails because it doesn't call the handler at all.
You could test this by writing a Console.WriteLine() in the handler and preserving the log: the handler is never called.
This means that you can't suppress the default behaviour by returning false.

This code will still result in the redirect:

<a href="#" class="btn btn-primary" onclick="@(() => { return Task.FromResult(false); })">Click me</a>

@rjmr97
Copy link

rjmr97 commented Apr 26, 2019

According to this dotnet/blazor#937 pull request, .preventDefault() is no longer called when you click on a link.

So it looks like we can fix #803 and all related issues trivially just be removing the event.preventDefault call. It doesn't appear to affect anything else negatively (...)

It is something you will be able to do in 3.0.0-preview6 according to this #5545 issue.

In the meantime, if it is possible in your situation, you can remove the href attribute from your tag and the event should work again.

@MarkSFrancis
Copy link
Author

MarkSFrancis commented Apr 26, 2019

Thanks @rjmr97, I'll close this ticket now as #5545 clearly covers this

@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants