✅ Blazor's @onclick isn't firing
I have a simple blazor server page with a couple of buttons. All the buttons have the same callback, where I put a Console.WriteLine to check if it does anything, but upon clicking them nothing happens. Anyone able to explain to me what's wrong? The console doesn't show any errors either. I'm using net8
15 Replies
I don't believe you need the preventDefault
tried without it, didn't work either
try changing up your formatting: https://learn.microsoft.com/en-us/aspnet/core/blazor/components/event-handling?view=aspnetcore-8.0#lambda-expressions
ASP.NET Core Blazor event handling
Learn about Blazor's event handling features, including event argument types, event callbacks, and managing default browser events.
i dont use blazor but it doesn't seem like you are using the lambda correctly here
I tried removing the method param and not using a lambda, but that didn't work either
do you use the other formatting
not sure what this curse is, I have the same problem with other new blazor projects. Even if the code is identical to the counter example
I use the
<button @onclick="OnClickCallback">
<button class="grid-item" @onclick="() => CopySecret(x)">
to
<button class="grid-item" @onclick="@(() => CopySecret(x))">
doesn't work
no web console error too
try calling it asynchronously since it is an async task
<button class="grid-item" onclick="@(async () => await CopySecret(x))">
I'll try when I get back home, although that doesn't sound like it'll do anything
otherwise like I said I don't use blazor so I'm unsure, but you could try some of these answers: https://stackoverflow.com/questions/58196812/blazor-onclick-event-is-not-triggered
Stack Overflow
Blazor onclick event is not triggered
I try to implement a simple onclick event handler like this sample, but it is not working in my solution. The event is only triggered at the run of the web page for unknown reasons.
The HTML page w...
Yeah I'm just clutching at straws because it should work even without async-await
You sure you set up your interactivity?
Blazor .net 8 requires it
Try putting
@rendermode InteractiveServer
on topthanks this fixed it