我有个问题。当我将事件处理程序从后面的代码添加到按钮时,该事件永远不会被触发。但是当我从创建按钮标签时添加它时,它可以完美地工作,我正在从后面的代码创建按钮并将它添加到表中。
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="show table" OnClick="Button1_OnClick" />
<table border="1">
<thead>
<tr>
</tr>
</thead>
<tbody id="tbody" runat="server">
</tbody>
</table>
</div>
</form>
protected void Button1_OnClick(object sender, EventArgs e)
{
var row = new TableRow();
var btnDownload = new Button { ID = "ID", Text = "Click Here" };
btnDownload.Click += ClickEvent;
var cell = new TableCell();
cell.Controls.Add(btnDownload);
row.Controls.Add(cell);
tbody.Controls.Add(row);
}
protected void ClickEvent(object sender, EventArgs e)
{
Debug.WriteLine(((Button)sender).Text);
}