Blazor call a method on Property Value Change

Viewed 2198

i am looking for a way to call a method when value of a property is change (Not By USER)

e.g i have this property

[Parameter]public int SelectedPatientId { get; set; }

and when ever its value changes i want to call this method

public async void valuechanged()
{
... do something here
}

so far i tried this and it's not working

<input type="hidden" value="@SelectedPatientId" @onchange="valuechanged"/>

<input type="hidden" @bind="@SelectedPatientId" @bind:event="valuechanged"/>

1 Answers

Call on the setter :

int _selectedPatientId;
[Parameter]public int SelectedPatientId 
{ 
    get => _selectedPatientId;
    set
    {
        _selectedPatientId = value;
        valuechanged();
    }
}

An asnc method should not return void but Task or ValueTask

public async Task valuechanged()
{
... do something here
    return Task.CompletedTask;
}
Related