C#C
C#3y ago
occluder

✅ 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

@page "/"
@inject IJSRuntime JSRuntime

<PageTitle>Home</PageTitle>

<div class="grid-container">
    @foreach (var x in secrets)
    {
        <button class="grid-item" @onclick="() => CopySecret(x)" @onclick:preventDefault>
            <h3>asdf</h3>
            <p>@x</p>
        </button>
    }
    
    <button class="add-grid-item">
        <h4>Add new</h4>
    </button>
</div>

@code
{
    private string[] secrets = ["xD", "kimne", "gp7xd", "lol", "kimne", "gp7xd", "lol", "kimne", "gp7xd", "lol"];


    private async Task CopySecret(string secret)
    {
        Console.WriteLine("test"); // This is never executed, even if the line below is commented out
        await JSRuntime.InvokeVoidAsync("clipboardCopy.copyText", secret);
    }
}
Was this page helpful?