0

我正在一些输入 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);
        }
    }

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

4

2 回答 2

0

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

于 2012-01-18T23:02:49.560 回答
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 });
    }
}
于 2012-01-18T23:09:25.880 回答