Harshit
Harshit

Reputation: 5157

Add click event on linkbutton

I have programatically generated linkbutton inside gridview. Here it is

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
     LinkButton lb = new LinkButton();
     LinkButton nlb = new LinkButton();

     lb.ID = "Ok";                    
     lb.Text = e.Row.Cells[3].Text;
     lb.Click += new EventHandler(TotalMinor_Click);
     e.Row.Cells[3].Controls.Add(lb);

     nlb.ID = "TotalMinorLink";
     nlb.Text = e.Row.Cells[4].Text;
     nlb.Click += new EventHandler(this.TotalMinor_Click);
     e.Row.Cells[4].Controls.Add(nlb);
}

protected void TotalMinor_Click(object sender, EventArgs e)
{

}

I expect to call TotalMinor_Click function on click on link, but it is not calling the function. What could be the reason ?

Upvotes: 0

Views: 51

Answers (2)

tynar
tynar

Reputation: 136

The case of events not being fired is ViewState issue and your control should exist and register events on OnInit or OnLoad events of ASP.NET Page LifeCycle. Maybe you should place a hidden div and put your controls inside this div. And in GridView_RowDataBound event handler just find those divs and toggle visibility.

Upvotes: 1

rjps12
rjps12

Reputation: 605

On your LinkButton inside the gridview, it should be like this..

 <asp:LinkButton runat="server" ID="lnkbtnID" Text='Text' OnClick="TotalMinor_Click" />

Upvotes: 0

Related Questions