How can I clear a text type Input field in my Blazor server app? The field is not cleared when I clear the related string variable

Viewed 28

I have a text type input field in my Blazor server app. With the "onchange" event, a function is executed. The value of the input string is transfered to the "data" variable.

I have also a disconnect button. When I press the disconnect button The data variable will be cleared. But I want also that the input field is cleared. But the last entered string remains in the input field. How can I clear it with the disconnect button?

<p>data: @data</p>
<button @onclick="Func_Disconnect">Disconnect</button>
<input type="text" @onchange="Eval_input">
@code {
  private string data { get; set; }

  private void Eval_input(ChangeEventArgs e)
  {
    data = (string)e.Value;
    // some code that evaluates data variable        
  }

  private void Func_Disconnect()
  {
    // some code
    data="";       
  }
 
}
1 Answers

You should set data value in input like this

This is your html

<input type="text" value=@data @onchange="Eval_input" />

This is your C# code

private void Eval_input(ChangeEventArgs e)
  {
    data = (string)e.Value;
    // some code that evaluates data variable        
  }

  private void Func_Disconnect()
  {
    // some code
    data="";       
  }
Related