2013-01-09 45 views
1

我正在创建一个动态表单,其中用户选择一个区域然后选择一个主题,然后根据主题加载一组控件。表单的选项全部在XML文件中定义。将在xml中定义的控件添加到asp.net面板

我的问题是,当我遍历XML时,我正在开始定义的控件,我在文本文件中创建了2个控件,每次创建两个控件。我认为最好是展示一些代码然后解释一下,这里是我的代码。

<?xml version="1.0" encoding="utf-8" ?> 
<options> 
    <option name="xerox"> 
    <sub_option name="Paper Jam"> 
    <input type="dropdown" name="location" /> 
    <input type="textbox" name="printername" /> 
    </sub_option> 
    <sub_option name="New Printer Request" /> 
    <sub_option name="Supply Request" /> 
    <sub_option name="Hardware Failure" /> 
</option> 
</options> 

及以下下面

protected void loadControls(string parent, string parentNode) 
    { 
     XmlDocument itemList = new XmlDocument(); 
     itemList.Load(@"c:\inetpub\wwwroot\sp\css\itemList.xml"); 
     Panel controls = new Panel(); 
     XmlNodeList nodeList = itemList.SelectNodes("options/child::node()"); 
     test.Text = parent; 
     foreach (XmlNode node in nodeList) 
     { 
      if (node.Attributes["name"].Value == parentNode && node.HasChildNodes) 
      { 
       test.Text = "for 2 coming"; 
       foreach (XmlNode subnode in node.ChildNodes) 
       { 
        if (subnode.Attributes["name"].Value == parent && subnode.HasChildNodes) 
        { 
         foreach (XmlNode optionNode in subnode.ChildNodes) 
         { 

          string controlType = optionNode.Attributes["type"].Value; 
          string controlName = optionNode.Attributes["name"].Value; 
          switch(controlType) 
          { 
           case "dropdown": 
            DropDownList ddl = new DropDownList(); 
            qna.Controls.Add(ddl); 
            break; 
           case "textbox": 
            TextBox tb = new TextBox(); 
            qna.Controls.Add(tb); 
            break;  
          } 

         } 
        } 
       } 
      } 


     } 

输出(这是选择卡纸后) output

回答

1

C#代码我想这个问题是在你的下面的代码行:

test.Text = "for 2 coming"; 
foreach (XmlNode subnode in node.ChildNodes) 
{ 

你应该检查下面的代替foreach循环这里

if(node.ChildNodes.Count>0) 
{ 
    ... 
} 
+0

我知道这很简单,我错过了。万分感谢! –

相关问题