Setting property Disabled to true in OnInitialized not working
Hi,
I have a Radio Button which, under certain conditions, has to be disabled.
In my codebehind file I have the next code:
private SfRadioButton<enmSelectedPerson> rbPartner = new();
protected override void OnInitialized()
{
rbPartner.Disabled = true;
}
During OnInitialized I see that property Disabled is set to True by the code but it loses this state and the property is again False after the page is loaded.
How can I set the property Disabled so that the Radio Button is, under certain conditions, disabled after page load?
Hi Vince,
We have reviewed your query along with the provided code snippet. The issue occurs because the OnInitialized method executes before the Radio Button component is rendered, so the Disabled property is not applied correctly.
To achieve the desired behavior, we recommend using the OnAfterRenderAsync lifecycle method or the Created event of the Radio Button component to set the Disabled property after the component has been rendered.
|
<SfRadioButton @ref="rbPartner" Label="Option 1" Name="options" Value="card" TChecked="string" Created="onCreated"></SfRadioButton>
@code{ private SfRadioButton<string> rbPartner = new();
protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { rbPartner.Disabled = true; StateHasChanged(); // Notify the UI about the state change } }
private void onCreated() { rbPartner.Disabled = true; } } |
Additionally, setting the component property using the component instance is not a proper way of property binding. Instead, define a private variable and bind it to the Disabled property of the Radio Button component.
|
<SfRadioButton @ref="rbPartner" Label="Option 1" Name="options" Value="card" TChecked="string" Disabled="@disabled"></SfRadioButton>
@code{ private SfRadioButton<string> rbPartner = new(); private Boolean disabled; protected override void OnInitialized() { disabled = true; } |
Please let us know if you need further assistance.
Regards,
YuvanShankar A
This works. Thanks
YuvanShankar
!
You are welcome, Vince. Please get back to us if you need any further assistance on this.
If that post is helpful, please consider accepting it as the solution so that other members can locate it more quickly.