Question

How to add OnClick event of dynamically added buttons in asp.net. I have added buttons dynamically and now I want to create an clickevent for those buttons.

 if (dtTasks.Rows[j]["EmpID"].Equals(dtEmployees.Rows[i]["EmpID"]))
 {
       TableRow r = new TableRow();
       TableCell[] cells =  new TableCell();
       Button btn = new Button();
       btn.ID = "btn" + dtTasks.Rows[j]["TaskID"].ToString();
       btn.Text = "Add Comment";
       btn.OnClientClick = "Click";
       cells.Controls.Add(btn);
 }
Was it helpful?

Solution

You can add the button's Click handler like this.

 btn.Click += new EventHandler(btnClick);

OTHER TIPS

You have to add the client side click event as:

btn.Attributes.Add("OnClick","return clientClick(this);");

where this will hold the button for your manipulation, if you don't need it, than don't pass it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top