How do I conditionally PreventDefault inside the input component in Blazor?
Nov 23, 2020
Blazor,
ASP .NET Core Blazor,
Blazor FAQ,
conditionally preventDefault input component blazor,
2263 Views
This code will help you to understand how to 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;
}
}
}

