Tabibi
September 15, 2020, 5:12pm
1
Hi!
I have a Blazor application with some Radzen components.
I want to add some RadzenButton dynamically in c# code (not html). I use builder.AddAttribute method to add Icon, Style and so on to the button without any problem. But when use builder.AddAttribute for "Click" event, I get this error on runtime:
Unhandled exception rendering component: Unable to set property 'Click' on object of type 'Radzen.Blazor.RadzenButton'. The error was: Specified cast is not valid.
`
korchev
September 15, 2020, 7:04pm
2
Hi @Tabibi ,
You can inspect the code which Blazor generates for a .razor page in such cases. Here is how it looks like for the Click event of a button:
__builder2.AddAttribute(12, "Click", Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<Microsoft.AspNetCore.Components.EventCallback<Microsoft.AspNetCore.Components.Web.MouseEventArgs>>(Microsoft.AspNetCore.Components.EventCallback.Factory.Create<Microsoft.AspNetCore.Components.Web.MouseEventArgs>(this,
(args) => Click(args, "Button with text")
)));
1 Like
Tabibi
September 16, 2020, 4:35am
3
Thank you very much!
It worked.
Good afternoon, I tried to do the same just for Tooltip and i faced such problem
builder.AddAttribute(6, "MouseEnter", Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<Microsoft.AspNetCore.Components.EventCallback<Microsoft.AspNetCore.Components.ElementReference>>(Microsoft.AspNetCore.Components.EventCallback.Factory.Create<Microsoft.AspNetCore.Components.ElementReference>(this,
(args) => ShowTooltip(args, new TooltipOptions() { Duration = 10000 })
)));
also tried using AddElementReferenceCapture but the problem remains
builder.AddElementReferenceCapture(6, (__ref) => { tooltipService.Open(__ref, Text, new TooltipOptions() { Duration = 10000 }); });
The error is self-explanatory. You are trying to use this
from a static method which won't work.
dut how then can I display the tooltip in this component
Our demo page contains plenty of examples. None of them uses static methods.