Search code examples
placeholderdynamic-controlsasp.net-controls

Dynamic Controls not Displaying


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.


Solution

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