Blazor

How do I conditionally PreventDefault inside the input component in Blazor?

How do I conditionally PreventDefault inside the input component in Blazor?

You can use the “preventDefault” attribute to prevent certain actions(events). In the following example, input keypress event checks whether the key value is “a” to prevent the keypress event.

<input value="@name" type="text" @onkeydown="@onKeydown" @onkeydown:preventDefault="@isPreventKey" />

@code {

    private string name { get; set; }

    private bool isPreventKey { get; set; }

    private void onKeydown(KeyboardEventArgs args)
    {
        if(args.Key == "a")
        {
            this.isPreventKey = true;
        }
    }
}
Sumitra Kumari Shaw

An Engineering graduate who Enjoys building things that live on the internet.