How to use builder.AddAttribute to add a "Click" event handler to a RadzenButton

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.


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

Thank you very much!
It worked.