内部事件不会被解雇 [英] inner event doesnt gets fired

查看:53
本文介绍了内部事件不会被解雇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS2010,并在运行时创建了一个按钮.但是,每当我单击运行时按钮(btnHello)时,都会调用OnClickMeClicked事件.有人可以告诉我为什么吗?以及如何触发内部事件?这是代码:-

I am using VS2010 and have created a button at runtime. But whenever I click on the runtime button (btnHello), the OnClickMeClicked event gets called. Can anybody tell me why ? And how to fire the inner event? Here is the code:-

public void OnClickMeClicked(object sender, EventArgs ea)
        {
            Response.Write("You Clicked My Button!");
            Button btnHello = new Button();
            btnHello.Text = "Hello";
            Page.Form.Controls.Add(btnHello);
            btnHello.Visible = true;
            btnHello.Click +=
                delegate
                {
                    Response.Write("i am newbutton x");
                };
        }




问候.




Regards.

推荐答案

您无法在那样的运行时创建按钮,每次触发Page_Load事件时,在运行时创建的按钮都会消失,解决方案是:您必须将已创建的按钮列表存储到List<>中.然后在页面"为加载"时,呈现List<>的按钮:)
You can''t created a button at runtime like that, everytime the Page_Load event fired, your button created at runtime will disappear, the solution is: you must be store a list of button you have been created into a List<> and then when the Page is Load, you render the List<> of Button :)


这篇关于内部事件不会被解雇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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