1
votes

I have a simple Blazor component which is textbox that has been bound to a property:

<input @bind="TheValue" />

@code {
    public string TheValue { get; set; } = "abc";

    public void FillTextBox(string s)
    {
        TheValue = s;
        Console.WriteLine("TheValue is = " + TheValue);
    }
}

And in my main page where I used the component I have the following code:

<MyTextbox @ref="myTextbox" /></MyTextbox>

<button @onclick="@(()=>myTextbox.FillTextBox("123"))">Fill</button>

@code {
     MyTextbox myTextbox;
}

When the component is rendered it has the default value of "abc" and if I call the FillTextBox inside the component it also work fine but when I click the button although the FillTextBox will be executed (and displays the value in the console) it does not change the input element's value. So to summarize the @bind does not work when I call the method that changes the binding variable .

Update: I'm seeking for data-binding or passing value to child components, I am looking for calling a previously-created child component method without having to be concerned about whether the component will render itself correctly.

1
isn't it '<input @bind-value="@TheValue" />' ? - Ryan Vettese
@RyanVettese I have tried this, still not working. - nAviD
This is pretty serious issue I think. It just should work. - Alamakanambra

1 Answers

1
votes

I think when your component is used like this the component does not rerender. Maybe adding a method with StateHasChanged and allso calling this method does the job?