Search code examples
c#asp.net-coreblazorblazor-webassembly

How to prevent default in Blazor?


I have this simple button in MudBlazor:

<MudIconButton Class="answer-button" Icon="@_icon" OnClick="OnClick" Color="@_color" 
                   @oncontextmenu="OnContextMenu"
                   @oncontextmenu:preventDefault="true"/>

I'm just trying to prevent right click action on button and apply my own logic to it.

I was using this article, but I get this build error

EventCalendarAnswer.razor(5,36): error RZ10010: The component parameter 'oncontextmenu' is used two or more times for this component. Paramete
rs must be unique (case-insensitive). The component parameter 'oncontextmenu' is generated by the '@oncontextmenu:preventDefault' directive attribute

How can I prevent default behaviour in Blazor?


Solution

  • You can wrap the MudIconButton inside a div and apply the @oncontextmenu attribute to the parent element.

    <div @oncontextmenu="OnContextMenu" @oncontextmenu:preventDefault="true">
        <MudIconButton Class="answer-button" Icon="@_icon" OnClick="OnClick" Color="@_color" />
    </div>