How to add onclick event of dynamically added buttons

How to add OnClick event of dynamically added buttons in asp.net. I added buttons dynamically, and now I want to create a clickevent for these 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); } 
+6
source share
3 answers

You can add a Click handler like this.

  btn.Click += new EventHandler(btnClick); 
+4
source

You must add a client side click event as:

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

where it will hold the button for your manipulations, if you do not need it, than not to transfer it.

+3
source

Create button in page load

  Button btn = new Button(); btn.Text = "Dynamic"; btn.Click += new EventHandler(btnClick); 

Click Event Button

 protected void btnClick(object sender, EventArgs e) { // Coding to click event } 
0
source

Source: https://habr.com/ru/post/949018/


All Articles