Search code examples
c#asp.net-corebindonchangeblazor-server-side

How can I perform a function related to a razor page input field's onchange event in Blazor Serv. App? (I have to use the input value in the function)


Hello I have an text input field in my Blazor razor page where the user has to give an Input string. I have to evaluate this input string during the user is typing the value. I have defined a function in the "onchange" event of the input object. But I don't know how I can use the input value in this function without using @bind. The main problem for me is that I cannot use @bind an @onchange at the same time. Normally I would bind the input value to variable.

<input type="text"  @onchange="Eval_input" >
@code {
private void Eval_input()
{
 // my function for evaluating the input strng    
 // here in my function I want to use the entered input string 
}
}

Solution

  • You can try to use ChangeEventArgsin Eval_input.(string)e.Value is the value of input.Here is a demo:

    <p>data: @data</p>
    <input type="text" @onchange="Eval_input">
    @code {
        private string data { get; set; }
      
        private void Eval_input(ChangeEventArgs e)
        {
            data = (string)e.Value;
            // my function for evaluating the input strng
            // here in my function I want to use the entered input string
        }
    }
    

    result:

    enter image description here