上的UpdatePanel添加触发器的动态添加dynamially控制 [英] Adding Triggers dynamically on UpdatePanel for dynamially added controls

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

问题描述

我加入阵列按钮动态简单的面板位于一个更新面板中,现在我想为UpdatePanel的触发器添加对这些按钮的点击事件。我的codes为如下:

I am Adding array Buttons to a simple panel dynamically which is located in an Update Panel, now I want to Add triggers for UpdatePanel on click event of these buttons. My codes is as below:

protected void AddButtons()
{
    Button[] btn = new Button[a];
    for (int q = 0; q < a; q++)
    {

        btn[q] = new Button();

        buttonsPanel.Controls.Add(btn[q]);
        btn[q].ID = "QID" + q;
        btn[q].Click += new EventHandler(_Default_Click);
        btn[q].Attributes.Add("OnClick", "Click(this)");

        AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
        trigger.ControlID = btn[q].ID;
        trigger.EventName = "Click";
        UpdatePanel2.Triggers.Add(trigger);                
    }
}

现在单击事件不会触发当我点击这些bottons和按钮都得到清除。

Now click event is not fired when i click on any of these bottons and buttons are getting removed.

请注意,这些按钮不可用在Page_Init()方法。

Please note that these buttons are not available on Page_Init() method.

推荐答案

您需要分配UniquID而不是ID来AsyncPostBackTrigger.ControlID财产。尝试使用下面的code:

You need to assign UniquID instead of ID to AsyncPostBackTrigger.ControlID property. Try to use the following code:

AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = btn[q].UniqueID;
trigger.EventName = "Click";
UpdatePanel2.Triggers.Add(trigger);

这篇关于上的UpdatePanel添加触发器的动态添加dynamially控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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