First of all, I reproduced you problem with the await Task.Delay(1000);
:
private async Task PushToDatabaseFunction() { await Task.Delay(1000); statusLabelBindString = $" {inputFieldString} was added to db";}
The code above can reproduce your problem. And I can fix it in two ways:
- Call the
StateHasChanged()
manually, so you can also try it:
if(db.status=="ok"){ statusLabelBindString = $" {inputFieldString} was added to db"; StateHasChanged();} else{ statusLabelBindString = "Error - Duplicate value"; StateHasChanged();}
- Delete the
await
inawait Task.Delay(1000);
:
private async Task PushToDatabaseFunction() { Task.Delay(1000); statusLabelBindString = $" {inputFieldString} was added to db";}
The two ways can bot update the label when you press the Enter first time.
So the problem is asynchronous method. UI is never refreshed (re-rendered) because the StateHasChanged()
is never being called by the runtime because the runtime didn't know when the async methods called return.