以编程方式创建按钮及其Click事件 [英] creating button programiticaly and its Click event

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

问题描述

当触发其他buuton Click事件时,我要以编程方式创建一个新按钮(b1).
创建了b1,但是当我单击b1时,b1变为隐藏状态,并且无法触发b1s事件.
我该怎么做才能正常工作
代码被打击:

when other buuton Click event fired, i want create a new button(b1) programiticaly.
b1 is created but when i Click b1 so b1 became hidden and i cant fired b1s event.
what i do so it work correctly
code is blow:

//*********************************************************************************

    protected void checkbutton_Click(object sender, EventArgs e)
    {
        Table tb = new Table();
        for (int k = 0; k < 4; k++)
        {
            TableRow tr = new TableRow();
            TableCell tch = new TableCell();
            tr.Cells.Add(tch);
            for (int j = 0; j < 10; j++)
            {
                TableCell tc = new TableCell();
                Button b1 = new Button();
                b1.Text = ((j + 1) + (k * 10)).ToString();
                b1.Click += new EventHandler(b1_Click);
                tc.Controls.Add(b1);
                tr.Cells.Add(tc);
            }
            tb.Rows.Add(tr);
        }
        place1.Controls.Add(tb);
    }

void b1_Click(object sender, EventArgs e)
{
    Button b2=(Button)sender;
    this.Title = b2.Text;
}

推荐答案


检查此
不触发动态创建按钮的单击事件 [ ^ ]
最好的问候
M.Mitwalli
Hi ,
Check this
not firing dynamically cretated button click event[^]
Best Regards
M.Mitwalli




您的按钮被隐藏,因为您的页面在单击后即被回发.您需要在 PageLoad/PageInit 中编写按钮创建代码才能正常工作.并且不要忘记也关联该按钮的事件.


谢谢
-Amit Gajjar
Hi,

Your button get hide because your page is postback when it clicked. You need to write your button creation code in PageLoad/PageInit to work. And don''t forget to associate event of that button as well.


Thanks
-Amit Gajjar


这篇关于以编程方式创建按钮及其Click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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