动态创建的按钮不触发Click事件 [英] Dynamically created button not firing Click event

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

问题描述

我创建动态控件,在一个控制是一个按钮。
我用下面的code以添加按钮控件。

I am creating dynamic controls, in that one control is a button. I used the following code to add the button control.

Button btnContinue = new Button();
btnContinue.Attributes.Add("class", "button");
btnContinue.ID = "btnContinue";
btnContinue.Text = "Continue";
btnContinue.CausesValidation = false;
btnContinue.Click += new EventHandler(btnContinue_Click);                
lineAdd.Controls.Add(btnContinue);

和下面的按钮单击事件。

And the button click event as below.

protected void btnContinue_Click(object sender, EventArgs e)
{
    ...
}

此事件不会开火。任何想法,这是为什么不触发事件。请纠正我,如果我错了。

This event is not firing. Any idea why this is not firing the event. Please correct me if i am wrong.

先谢谢了。

推荐答案

请确保您所创建的按钮做得早在页面生命周期。看把它创建的页面的OnInit的。

Make sure your are creating that button early enough in the page lifecycle. Look to get it created OnInit of the Page.

这是因为该事件处理事件的页面初始化后发生。当然按钮具有前可以在其上检测到的事件已被创建。 Web表单的喜悦...

This is because the event handling events happen after Page Init. Of course the button has to have been created before the events can detected on it. The joy of web forms...

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

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