动态控件不显示

时间:2012-01-18 22:58:00

标签: placeholder dynamic-controls asp.net-controls

我正在创建一些输入XML的控件。 然后将控件添加到位于表中的不同PlaceHolder控件中。以下是参考代码

private void RenderFactorControls(string xml)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xml);

        foreach (XmlNode xmlNode in xmlDoc.DocumentElement.ChildNodes)
        {
            CheckBox factorCheckBox = new CheckBox();
            factorCheckBox.ID = "chkBox"+xmlNode.Attributes["id"].Value;
            factorCheckBox.Text = xmlNode.Attributes["id"].Value;

           this.pholderControls1.Controls.Add(factorCheckBox);
           this.pholderControls2.Controls.Add(factorCheckBox);
           this.pholderControls3.Controls.Add(factorCheckBox);
           this.pholderControls4.Controls.Add(factorCheckBox);
           this.pholderControls5.Controls.Add(factorCheckBox);
        }
    }

只有最后一个占位符才会显示控件。

2 个答案:

答案 0 :(得分:0)

您只创建了一个CheckBox,并尝试将其添加到多个占位符。将控件添加到容器会将其从以前的父级中删除。尝试创建5个不同的复选框。

答案 1 :(得分:0)

private void RenderFactorControls(string xml)
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xml);

    foreach (XmlNode xmlNode in xmlDoc.DocumentElement.ChildNodes)
    {
        string id = "chkBox"+xmlNode.Attributes["id"].Value;
        string text = xmlNode.Attributes["id"].Value;

        this.pholderControls1.Controls.Add(new CheckBox() { ID = id, Text = text });
        this.pholderControls2.Controls.Add(new CheckBox() { ID = id, Text = text });
        this.pholderControls3.Controls.Add(new CheckBox() { ID = id, Text = text });
        this.pholderControls4.Controls.Add(new CheckBox() { ID = id, Text = text });
        this.pholderControls5.Controls.Add(new CheckBox() { ID = id, Text = text });
    }
}