asp.net按钮事件没有触发 [英] asp.net button event not firing

查看:88
本文介绍了asp.net按钮事件没有触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题。当我从代码后面添加一个事件处理程序到一个按钮时,事件永远不会被触发。但是当我在创建按钮标签时添加它时,它完美地工作,我从后面的代码创建按钮,然后将其添加到表格。

I have a problem. While I am adding an event handler from code behind to a button the event never gets fired. But when I add it from when creating the button tag it works perfectly and I am creating the button from code behind and I adding it to table.

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

推荐答案

备份你的按钮事件代码...从设计模式中删除按钮...添加新的按钮并双击它并粘贴代码...这将解决问题......
Take backup of your button event code... delete the button from design mode... add new button and double click it and paste the code... This will fix the issue...


这篇关于asp.net按钮事件没有触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆