如何添加动态添加按钮的onclick事件

时间:2022-10-22 07:53:17

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.

如何在asp.net中添加动态添加按钮的OnClick事件。我已经动态添加了按钮,现在我想为这些按钮创建一个clickevent。

 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);
 }

3 个解决方案

#1


5  

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

您可以像这样添加按钮的Click处理程序。

 btn.Click += new EventHandler(btnClick);

#2


3  

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.

如果你不需要它,它将保持你的操作按钮,而不是通过它。

#3


0  

Create Button in page load

在页面加载中创建按钮

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

Button Click Event

按钮单击事件

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

#1


5  

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

您可以像这样添加按钮的Click处理程序。

 btn.Click += new EventHandler(btnClick);

#2


3  

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.

如果你不需要它,它将保持你的操作按钮,而不是通过它。

#3


0  

Create Button in page load

在页面加载中创建按钮

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

Button Click Event

按钮单击事件

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