2012-01-18 80 views
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); 
     } 
    } 

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

回答

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

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