---
title: "Why link button does not fire click event on first time click when add dynamicly?"  
description: "Why link button does not fire click event on first time click when add dynamicly?"  
author: "Anonymous User"  
published: 2014-10-06  
updated: 2014-10-06  
canonical: https://www.mindstick.com/forum/2307/why-link-button-does-not-fire-click-event-on-first-time-click-when-add-dynamicly  
category: "c#"  
tags: ["c#", "asp.net"]  
reading_time: 2 minutes  

---

# Why link button does not fire click event on first time click when add dynamicly?

I am dynamically adding Link Buttons to my [Gridview](https://www.mindstick.com/articles/873/update-delete-edit-gridview-in-asp-dot-net) as seen below:

```
protected void addLinks()        {            foreach (GridViewRow gvr in gvData.Rows)            {                if (gvr.RowType == DataControlRowType.DataRow)                {                    string itemNbr =
gvr.Cells[1].Text;                    LinkButton lb = new LinkButton();                    lb.Text = itemNbr;                    lb.Click +=
genericLinkButton_Click;                    foreach (Control ctrl in gvr.Cells[1].Controls)                    {                       
gvr.Cells[1].Controls.Remove(ctrl);                    }                   
gvr.Cells[1].Controls.Add(lb);                }            }        }
```

This addLinks() [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) is called in my gridview_RowDataBound event and the [Page Load](https://www.mindstick.com/articles/12909/how-to-open-first-time-popup-on-page-load-in-website) event if(isPostPack).

The [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) is that when I click the link buttons, the genericLinkButton_Click event does not get fired on my first click. It [causes](https://yourviews.mindstick.com/view/293/renovation-causes-wastage-of-resources) a [postback](https://www.mindstick.com/blog/342/postback-in-asp-dot-net), and then if I click it again, or click one of the other Link Buttons, the genericLinkButton_Click event is fired.

How can I make sure the [click event](https://www.mindstick.com/forum/424/how-to-call-form_paint-event-on-button-click-event) happens on my first click?

## Replies

### Reply by Anonymous User

When using Webforms and dynamically creating controls you are required to assign an ID to the created control before adding the control to the tree in order for them to work properly. Otherwise they will have changing ID's during the pagelifecycle resulting in the described behaviour.

```
private int _runningIndex = 0;        protected void gvData_RowCreated(Object sender, GridViewRowEventArgs e)        {            if (e.Row.RowType == DataControlRowType.DataRow)            {                string itemNbr = e.Row.Cells[1].Text;                LinkButton lb = new LinkButton();                lb.ID = "btn" + (_runningIndex++).ToString();                lb.Text = itemNbr;                lb.Click += genericLinkButton_Click;                foreach (Control ctrl in e.Row.Cells[1].Controls)                {                    e.Row.Cells[1].Controls.Remove(ctrl);                }                e.Row.Cells[1].Controls.Add(lb);            }        }
```


---

Original Source: https://www.mindstick.com/forum/2307/why-link-button-does-not-fire-click-event-on-first-time-click-when-add-dynamicly

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
