7

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);
 }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sana Khan
  • 108
  • 1
  • 1
  • 7

2 Answers2

5

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

 btn.Click += new EventHandler(btnClick);
Sachin
  • 40,216
  • 7
  • 90
  • 102
4

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.

Imran Balouch
  • 2,170
  • 1
  • 18
  • 37