I'm familiar with C#/asp.net mvc (10+ years) but new with Blazor (10days). Now I want to add a "Change variable value on input key press" effect to MudBlazor's TextField.
Thanks for information here Change variable value on input key press on Blazor, it's quite easy to deal with a normal input or textarea. Please see my code here:
<MudGrid>
<MudItem lg="6">
<MudInputLabel Style="z-index: 99;">@_localizer["Script"]</MudInputLabel>
<textarea @bind="Script" @bind:event="oninput" cols="25" rows="10">@Script</textarea>
</MudItem>
<MudItem lg="6">
<MudTextField T="string"
Label="@_localizer["New data sources identified from current script"]"
Variant="Variant.Outlined" ReadOnly="true"
Text="@DataSourceIdsTextFromScript" Lines="10"/>
</MudItem>
</MudGrid>
When the value of the area (Script) is updated by @bind:event="oninput", CalculateDataSourceIdsTextFromScript() will update the value of DataSourceIdsTextFromScript, and the text in the second MudTextField will reflect the change.
public string Script
{
get => _script;
set
{
_script = value;
CalculateDataSourceIdsTextFromScript();
}
}
But when I tried to use a MudTextField instead of the textarea, bind-value / bind-text / OnInternalInputChanged could not do the same work.
@* <MudTextField T="string" Label="@_localizer["Script"]" bind-Value="Script" *@
@* OnInternalInputChanged="CalculateDataSourceIdsTextFromScript" *@
@* Variant="Variant.Outlined" Text="@Script" Lines="10"/> *@
On the other hand, @bind:event="oninput" cannot be applied to a MudTextField. I think it's because MudTextField is not the input/textarea but a shell around it.
So, is there a way to do that?
Thanks for any answers or tips.
While using a setter in a property is a perfectly valid option, Blazor is inherently asynchronous. Using a property setter ties you to the synchronous world.
You can implement async behaviour using the same pattern as used in the
@bind-value:after
used in the standardInputBase
like this.