WebForm中动态添加的控件不会触发事件 [英] Event doesn't fire for dynamically added controls in WebForm

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

问题描述

我在C#Web表单上有一个用户控件.该控件由另一个Activex控件组成.用户控件被动态添加到表单中.我为每个动态添加的用户控件实例添加了一个双击事件.但是,双击甚至不会被触发.下面是我的代码

I have a User Control on my C# web form. The Control in made of another Activex control. The user control is being added dynamically to the form. I have added a double click event for each dynamical added instance of user control. However the double click even is not getting fired. Below is my code

          int a = 0;
        int b = 0;
        int i, j;
        int[,] Matrica;

        Matrica = new int[n, m];
        for (i = 0; i < n; i++)
        {
            a = merlokacionin.X + 5;

            for (j = 0; j < m; j++)
            {
                textbox[i, j] = new UserCont();    
                textbox[i, j].Location = new Point(a, b)
                panel3.Controls.Add(textbox[i, j])
                textbox[i, j].DoubleClick += new System.EventHandler(dicom_DoubleClick);
                a = a + 105;
            }
            b = b + 105;

    }

   private void dicom_DoubleClick(object sender, System.EventArgs e)
    {
        MessageBox.Show("do something here");
    }



   //I have also tried; 
        int a = 0;
        int b = 0;
        int i, j;
        int[,] Matrica;
        Matrica = new int[n, m];
        for (i = 0; i < n; i++)
        {
            a = merlokacionin.X + 5;

            for (j = 0; j < m; j++)
            {
                textbox[i, j] = new UserCont();    
                textbox[i, j].Location = new Point(a, b)
                panel3.Controls.Add(textbox[i, j])
                textbox[i, j].DoubleClick += new    System.EventHandler(dicom_DoubleClick);
                a = a + 105;
            }
            b = b + 105;

        }

    private void dicom_DoubleClick(object sender, System.EventArgs e)
    {
        MessageBox.Show("do something here");
    }

我什至尝试在用户控件本身中添加双击事件,甚至没有被触发.

I have even tried adding Double click click event in the User control itself, and even that is not being fired.

在此方面的任何帮助都将受到高度赞赏.

Any help on this is highly appreciated.

推荐答案

在动态创建控件时,必须在不晚于的情况下重新添加控件,方法是 Page_Load 方法.否则,事件处理程序将不会重新连接",也不会为控件还原视图状态.

When working dynamically created controls, you must re-add the control no later than the Page_Load method. Otherwise, the event handlers will not "reconnect" nor will the viewstate be restored for the control.

OnInit 期间重新创建控件是页面生命周期中的正确"位置.虽然最终结果相同,但是在 Page_Load 期间重新创建控件会强制页面播放追赶".

Recreating the controls during OnInit is the "correct" place during the page's lifecycle. While the end result is the same, recreating the controls during Page_Load forces the page to play "catch up".

如果您可以从页面中提供相关代码,我可以为您提供实施.否则,只需重新创建动态控件即可,如上所述.

If you can provide the relevant code from your page, I can provide you with the implementation. Otherwise, just recreate the dynamic control(s) as I detailed above.

编辑

还有什么可以做的来触发事件吗?

Is there anything else that can be done to fire up event?

答案简短?不,没有.您受asp.net页面生命周期的束缚.如果不按上述方法重新创建控件,则将无法连接按钮的click事件.

Short answer? No, there isn't. You are bound by the asp.net page's lifecycle. If you don't recreate the controls as mentioned above, you will not be able to wire up the button's click event.

此处的窍门是跟踪动态创建的控件,以便您可以 Page_Init 期间还原它们.您可以使用ViewState来执行此操作.在按钮单击事件上添加控件时,请将重新创建控件所需的信息添加到页面的viewstate中.

The trick here is tracking the controls you have created dynamically so you can restore them during Page_Init. You can use ViewState to do this. When you add the controls on the button click event, add the info needed to recreate the controls into the page's viewstate.

可以在这里找到关于这些原理的很好的讨论:管理动态创建的控件-利用动态状态自动维护功能进行动态控制.

A good discussion of these principles can be found here: Manage Dynamically Created Controls - Take advantage of automatic state maintenance for dynamic controls.

要跟踪动态创建的控件,必须使用持久性数据存储,例如视图状态.您可以采用的一个简单技巧包括在创建动态控件时向页面的视图状态添加一些信息.添加到视图状态的语法和语义完全取决于您.

To track the controls created dynamically, you must use a persistent data store, such as the view state. A simple trick you can employ consists of adding some information to the page's view state whenever a dynamic control is created. The syntax and semantics of what you add to the view state are completely up to you.

这篇关于WebForm中动态添加的控件不会触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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