Dynamic Controls not Displaying

339 Views Asked by At

I am creating controls on some input XML. The controls are then added to the different PlaceHolder Control which is places in a table. Here is the code for reference

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);
        }
    }

Only the last place holder shows the controls.

2

There are 2 best solutions below

0
On BEST ANSWER
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 });
    }
}
0
On

You created only One CheckBox and are trying to add it to multiple placeholders. Adding a control to a container removes it from its previous parent. Try creating 5 different checkboxes.