Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add onclick event of dynamically added buttons

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);
 }
like image 336
Sana Khan Avatar asked Jul 09 '13 12:07

Sana Khan


People also ask

How do you add onclick event dynamically?

Attaching the event dynamically className = 'dynamic-link'; // Class name li. innerHTML = dynamicValue; // Text inside $('#links'). appendChild(li); // Append it li. onclick = dynamicEvent; // Attach the event!

Can you add multiple onclick events?

The first way to perform multiple onClick events in React is to write your logic in a single function, and then call that function inside of the onClick event handler.

Can we use Onclick in button tag?

The onclick attribute is an event attribute that is supported by all browsers. It appears when the user clicks on a button element. If you want to make a button onclick, you need to add the onclick event attribute to the <button> element.


2 Answers

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

 btn.Click += new EventHandler(btnClick);
like image 86
Sachin Avatar answered Nov 08 '22 23:11

Sachin


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.

like image 30
Imran Balouch Avatar answered Nov 08 '22 23:11

Imran Balouch