Users Pricing

forum

home / developersection / forums / why link button does not fire click event on first time click when add dynamicly?

Why link button does not fire click event on first time click when add dynamicly?

Anonymous User 2113 06 Oct 2014
I am dynamically adding Link Buttons to my Gridview 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 is called in my gridview_RowDataBound event and the Page Load event if(isPostPack).

The problem is that when I click the link buttons, the genericLinkButton_Click event does not get fired on my first click. It causes a postback, 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 happens on my first click?


I am a content writter !


1 Answers