动态生成的按钮,点击上未执行 [英] Dynamically generated buttons, on click not being executed

查看:129
本文介绍了动态生成的按钮,点击上未执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要生成按钮的动态列表,我已经做到了,与连接到它的事件处理程序。

但是没有被执行的事件处理程序。

 私人无效GetOptions(EcoBonusRequest要求)
        {
            变种ecobonuswworkflow = WorkflowFactory.CreateEcobonusWorkflow();
            ecobonuswworkflow.SetCurrentStep(request.CurrentStatus);
            变种currentoptions = ecobonuswworkflow.GetCurrentOptions();
            的foreach(在currentoptions VAR选项)
            {
                VAR BTN =新按钮(){文字= option.OptionName};
                btn.Click + =新的EventHandler(btn_Click);
                Buttons.Controls.Add(BTN);
            }        }
        无效btn_Click(对象发件人,EventArgs的发送)
        {
            VAR BTN =(按钮)发送;
            字符串命令= btn.Text;
            EcoBonusRequest请求= this.GetDBRequest(RequestBaseId.Value);
            EcoBonusRequestBL.AddWorkflowHistoryItem(请求,命令的currentUser,命令);
        }


解决方案

这是你在你的页面动态添加控件必须在Page_init事件增加,并且必须具有唯一的ID。如果要添加文本框或其他一些控制在用户可输入或变化值,不是每一个岗位回来时,这些控件重新添加它们必须有相同的ID。

I need to generate a dynamic list of buttons, I already did, with an event handler attached to it.

However the event handler is not being executed.

private void GetOptions(EcoBonusRequest request)
        {
            var ecobonuswworkflow = WorkflowFactory.CreateEcobonusWorkflow();
            ecobonuswworkflow.SetCurrentStep(request.CurrentStatus);
            var currentoptions = ecobonuswworkflow.GetCurrentOptions();
            foreach(var option in currentoptions)
            {
                var btn = new Button() {Text = option.OptionName};
                btn.Click +=new EventHandler(btn_Click);
                Buttons.Controls.Add(btn);
            }

        }


        void btn_Click(object sender, EventArgs e)
        {
            var btn = (Button) sender;
            string command = btn.Text;
            EcoBonusRequest request = this.GetDBRequest(RequestBaseId.Value);
            EcoBonusRequestBL.AddWorkflowHistoryItem(request, command,CurrentUser, command);
        }

解决方案

The controls that you add dynamically in your page must be added in Page_init event, and they must have unique Ids. If you are adding textboxes or some other controls where user can input or change value, than on every post back when these controls are re-added they must have same IDs.

这篇关于动态生成的按钮,点击上未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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