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

**URL:** <https://forum.radzen.com/t/how-to-use-builder-addattribute-to-add-a-click-event-handler-to-a-radzenbutton/4977>\
**Category:** Radzen.Blazor Components\
**Created:** [September 15, 2020, 5:12pm UTC](https://forum.radzen.com/t/how-to-use-builder-addattribute-to-add-a-click-event-handler-to-a-radzenbutton/4977 "2020-09-15T17:12:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tabibi](https://forum.radzen.com/user_avatar/forum.radzen.com/tabibi/32/4482_2.png) [@Tabibi](https://forum.radzen.com/u/Tabibi)\
**Post date:** [September 15, 2020, 5:12pm UTC](https://forum.radzen.com/t/how-to-use-builder-addattribute-to-add-a-click-event-handler-to-a-radzenbutton/4977/1 "2020-09-15T17:12:03Z")

</div>

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.`

`

---

<div class="post-metadata">

**Author:** ![korchev](https://forum.radzen.com/user_avatar/forum.radzen.com/korchev/32/4_2.png) [@korchev](https://forum.radzen.com/u/korchev)\
**Post date:** [September 15, 2020, 7:04pm UTC](https://forum.radzen.com/t/how-to-use-builder-addattribute-to-add-a-click-event-handler-to-a-radzenbutton/4977/2 "2020-09-15T19:04:00Z")

</div>

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:

```auto
__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")
  )));

```

---

<div class="post-metadata">

**Author:** ![Tabibi](https://forum.radzen.com/user_avatar/forum.radzen.com/tabibi/32/4482_2.png) [@Tabibi](https://forum.radzen.com/u/Tabibi)\
**Post date:** [September 16, 2020, 4:35am UTC](https://forum.radzen.com/t/how-to-use-builder-addattribute-to-add-a-click-event-handler-to-a-radzenbutton/4977/3 "2020-09-16T04:35:38Z")

</div>

Thank you very much!  
It worked.

---

<div class="post-metadata">

**Author:** ![Vitaliy\_Tkachenko](https://forum.radzen.com/user_avatar/forum.radzen.com/vitaliy_tkachenko/32/5065_2.png) [@Vitaliy\_Tkachenko](https://forum.radzen.com/u/Vitaliy_Tkachenko)\
**Post date:** [March 17, 2021, 11:40am UTC](https://forum.radzen.com/t/how-to-use-builder-addattribute-to-add-a-click-event-handler-to-a-radzenbutton/4977/4 "2021-03-17T11:40:52Z")

</div>

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 })
    )));

```

![image](https://forum.radzen.com/uploads/default/original/2X/3/3cce6dfd37ab9eccc43e9a9d58177ecb4771cb32.png)

![image](https://forum.radzen.com/uploads/default/original/2X/5/586b2708e36748cf175cb55e1d0b1f4dd76c4150.png)

---

<div class="post-metadata">

**Author:** ![Vitaliy\_Tkachenko](https://forum.radzen.com/user_avatar/forum.radzen.com/vitaliy_tkachenko/32/5065_2.png) [@Vitaliy\_Tkachenko](https://forum.radzen.com/u/Vitaliy_Tkachenko)\
**Post date:** [March 17, 2021, 12:32pm UTC](https://forum.radzen.com/t/how-to-use-builder-addattribute-to-add-a-click-event-handler-to-a-radzenbutton/4977/5 "2021-03-17T12:32:34Z")

</div>

also tried using AddElementReferenceCapture but the problem remains

```
builder.AddElementReferenceCapture(6, (__ref) => { tooltipService.Open(__ref, Text, new TooltipOptions() { Duration = 10000 }); });

```

![image](https://forum.radzen.com/uploads/default/original/2X/4/4ae43c32b3b98444cbc8d27c7122bc3aeccfe802.png)

---

<div class="post-metadata">

**Author:** ![korchev](https://forum.radzen.com/user_avatar/forum.radzen.com/korchev/32/4_2.png) [@korchev](https://forum.radzen.com/u/korchev)\
**Post date:** [March 17, 2021, 1:38pm UTC](https://forum.radzen.com/t/how-to-use-builder-addattribute-to-add-a-click-event-handler-to-a-radzenbutton/4977/6 "2021-03-17T13:38:24Z")

</div>

The error is self-explanatory. You are trying to use `this` from a static method which won't work.

---

<div class="post-metadata">

**Author:** ![Vitaliy\_Tkachenko](https://forum.radzen.com/user_avatar/forum.radzen.com/vitaliy_tkachenko/32/5065_2.png) [@Vitaliy\_Tkachenko](https://forum.radzen.com/u/Vitaliy_Tkachenko)\
**Post date:** [March 17, 2021, 1:51pm UTC](https://forum.radzen.com/t/how-to-use-builder-addattribute-to-add-a-click-event-handler-to-a-radzenbutton/4977/7 "2021-03-17T13:51:29Z")

</div>

dut how then can I display the tooltip in this component  
 ![image](https://forum.radzen.com/uploads/default/original/2X/b/bebf1a901952a7eed47f58619f4654cfe4ab01c1.png)

---

<div class="post-metadata">

**Author:** ![korchev](https://forum.radzen.com/user_avatar/forum.radzen.com/korchev/32/4_2.png) [@korchev](https://forum.radzen.com/u/korchev)\
**Post date:** [March 17, 2021, 1:57pm UTC](https://forum.radzen.com/t/how-to-use-builder-addattribute-to-add-a-click-event-handler-to-a-radzenbutton/4977/8 "2021-03-17T13:57:11Z")

</div>

Our [demo page](https://blazor.radzen.com/tooltip) contains plenty of examples. None of them uses static methods.

---

<div class="post-metadata">

**Author:** ![DonnieRCrump](https://forum.radzen.com/user_avatar/forum.radzen.com/donniercrump/32/21341_2.png) [@DonnieRCrump](https://forum.radzen.com/u/DonnieRCrump)\
**Post date:** [May 17, 2025, 2:28pm UTC](https://forum.radzen.com/t/how-to-use-builder-addattribute-to-add-a-click-event-handler-to-a-radzenbutton/4977/9 "2025-05-17T14:28:53Z")

</div>

I know this is super old but did you find a solution to this? I am running into the same error.
