Search code examples
asp.net-coreblazorblazor-server-side.net-5asp.net-blazor

Set value for input which is source of onchange event in Blazor


In Blazor Server App / .NET 6 preview 4 (same for .NET 5) I would like to handle onchange event and in certain cases to dismiss user input with setting a certain value to the textbox. There is a basic code for demonstation:

<input value="@tester" @onchange="@OnChangeHandler" />

@code {

    public string tester { get; set; }

    public void OnChangeHandler(ChangeEventArgs obj)
    {
        tester = "100";
    }
}

For some reason input shows "100" only once even though the property tester is set each time when OnChangeHandler fired. input keeps a value entered by the user ignoring the fact that tester is set to "100" when onchange occurs second and third time. How can I make input always contain actual value of tester?


Solution

  • A quick dirty workaround would be something like this :

      public async Task OnChangeHandler(ChangeEventArgs obj)
        {
            tester =null;
            await Task.Delay(1); 
            tester = "100";
        }
    

    it's not good a fix is planned in the preview 5 of dotnet6