动态(编程)将复选框和的CheckedChanged事件 [英] Dynamically (programatically) adding check boxes and checkedchanged events

查看:148
本文介绍了动态(编程)将复选框和的CheckedChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点增加一些复选框的问题和事件处理程序编程。复选框都出现罚款,但点击时他们没有做任何事情。没有任何人有任何想法,我做错了?

我的code:

 的foreach(声明我theseStatements)
        {
            箱=新的复选框();
            box.Text = i.StatementText;
            box.AutoPostBack = TRUE;
            box.CheckedChanged + =新的EventHandler(this.CheckedChange);
            PlaceHolder.Controls.Add(盒);
        }保护无效CheckedChange(对象发件人,EventArgs的发送)
    {
        复选框X =(复选框)发送;        Instructions.Text =改变;        WorkPlaceHazardsBox.Text + = x.Text;
    }


解决方案

您应该做到以下几点:


  1. ID 属性您在的foreach <创建复选框的每个实例/ code>循环。

  2. 对于回传,确保您的复选框被创建和的CheckedChanged 事件处理程序附着在页面生命周期的某个点的的控制事件上调

I am having a bit of a problem adding a few check boxes and an event handler programatically. The check boxes all appear fine, but they don't do anything when clicked. Does anyone have any idea what I am doing wrong?

My code:

foreach (Statement i in theseStatements)
        {
            box = new CheckBox();
            box.Text = i.StatementText;
            box.AutoPostBack = true;
            box.CheckedChanged += new EventHandler(this.CheckedChange);
            PlaceHolder.Controls.Add(box);
        }

protected void CheckedChange(object sender, EventArgs e) 
    {
        CheckBox x = (CheckBox)sender;

        Instructions.Text = "change";

        WorkPlaceHazardsBox.Text += x.Text;
    }

解决方案

You should do the following:

  1. Set the ID property for each instance of CheckBox you create in your foreach loop.
  2. For PostBacks, ensure that your CheckBoxes are created and CheckedChanged event handler is attached at some point of the page life-cycle before control events are raised

这篇关于动态(编程)将复选框和的CheckedChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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