I want to change the child content of a button after click, but I got some error when I try.
This code work but, obviusly, don’t render the icon passed:
<TelerikButton OnClick="@RevealPassword"
Class="pass-btn"
Primary="true"
ButtonType="ButtonType.Button"
Id="btnShowPwd"
Title="Show">
@EyeIcon
</TelerikButton>
@code {
[Parameter]
public string EyeIcon { get; set; } = "<i class='fal fa-eye fa-lg'></i>";
public async Task RevealPassword()
{
EyeIcon = "<i class='fal fa-eye-slash fa-lg'></i>";
StateHasChanged();
HidePassword = false;
await Task.Delay(2000);
HidePassword = true;
EyeIcon = "<i class='fal fa-eye fa-lg'></i>";
StateHasChanged();
}
[Parameter] public EventCallback<string> OnTypePassword { get; set; }
}
Button work and change correclty the child content after click
But when I try to convert with @((MarkupString)myVariable) I got an error:
<TelerikButton OnClick="@RevealPassword"
Class="pass-btn"
Primary="true"
ButtonType="ButtonType.Button"
Id="btnShowPwd"
Title="Show">
@((MarkupString)@EyeIcon)
</TelerikButton>
Why?
3
Answers
I found a workaround to solve my problem. Here is the code:
View:
@Code:
CSS:
It's not the best solution, but it's work fine.
Change type of
EyeIcon
from string toRenderFragment
:In parent create var string to hold you css:
In you method
RevealPassword()
change the value of youCssStyle
.In
TelerikButton
you need to writeEyeIcon
like this:The Telerik button is wanting a
RenderFragment
of which aMarkupString
is not able to be converted into.Instead, try something like
with whatever switching logic you want in there. See more details here.
That said, I think Telerik has an option to specify an icon as part of the component so you may want to try that path first as that’s a bit cleaner.