为动态创建的按钮生成事件处理程序 [英] generating the event handlers for dynamically created buttons

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

问题描述

hi



我创建了动态按钮,并向其中添加了一个事件处理程序,并且该按钮以最佳方式工作.


我的示例代码是:

hi



i had created the dynamic button and added an event handler to it , and it was working in the best way.


my sample code is:

Button btn = new Button();
     btn.ID = "btn";
     btn.Text = "Button";
     btn.Click += new System.EventHandler(btn_Click);

     this.form1.Controls.Add(btn);



在单击按钮时,iam会获得良好的结果


我的问题是
我已经创建了表并使用C#.net编程动态添加了按钮,这里一切都很好,我也生成了事件处理程序,但是它不起作用

示例代码:



here iam getting the good result when button is clicked


my problem is
i had create the table and added the buttons dynamically with C#.net programming, and everything is fine here,i had generated the eventhandler also, but it is not working

sample code:

TableCell cell = new TableCell();
                if (j == 0)
                {
                    Button b = new Button();
                    b.Text = dt.Rows[i][j].ToString();
                    b.ID = i + "," + j+"Button";
                    b.Click += new EventHandler(b_Click);
                    cell.Controls.Add(b);
                    row.Cells.Add(cell);
                }




所以请帮帮我


谢谢Advanced




so please help me


thanks in advanced

推荐答案

我猜测您不是在回发时重新创建动态表,按钮等.

需要重新创建动态添加的控件,以使回发方法和视图状态正确运行.
I am guessing that you are not recreating the dynamic table, button etc on postback.

Dynamically added controls need to be recreated in order for postback methods and viewstate to function correctly.


这篇关于为动态创建的按钮生成事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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