2012-08-06 25 views
0

在我的表单中,我设置了2个字段。项目的位置和子区域。我试图根据用户选择的位置填充子区域字段。例如,他们选择“地点4”。这个位置只有3个子区域,可以说是A,B和C.在位置框中选择后,子区域框将只显示A,B和C.我拥有所有位置并允许一个名为appsettings.xml的xml文件中的子区域。如何让程序读取xml文件并允许子区域只填充有效数据?以下是我如何设置我的xml文件的示例。C#:根据用户选择使用XML文件填充表单域

<?xml version="1.0" encoding="utf-8" ?> 
<MerchandiseTrack> 

    <Merchandise_Zones Application_Data="Test-Run"> 

     <KeyBoard_App> 
     <AppString>c windows osk.exe</AppString> 
     </KeyBoard_App> 

<Storage_Location> 

    <head id="Location">    // Name of box on app 
     <body id="04">     // Name of Location within the box 
     <Sub-Area>A, B, C,</Sub-Area> // Allowed sub-areas 
     </body> 
    </head> 

    <head id="Location">    // Name of box on app 
     <body id="05">      //Name of Location within the box 
     <Sub-Area>P, L, R, B</Sub-Area> // Allowed sub-areas 
     </body> 
    </head> 

    <head id="Location">    // Name of box on app 
     <body id="14">      //Name of Location within the box 
     <Sub-Area>A, X, C </Sub-Area>  //Name of Location within the box 
     </body> 
    </head> 

    </Storage_Location> 
</Merchandise_Zones> 
</MerchandiseTrack> 

回答

1

您在SelectedIndexChanged上设置了活动。然后你读了locationID并从文件中选择节点:(!)。

 XmlDocument doc = new XmlDocument(); 
     doc.Load(@"path/to/file.xml"); 
     XmlNode subarea = doc.SelectSingleNode("/MerchandiseTrack/Merchandise_Zones/Storage_Location/head/body[@id=" + locationComboBox.SelectedItem.ToString()+ "]/Sub-Area"); 
     string[] areas = subarea.InnerText.Split(','); 
     foreach (string area in areas) 
     { 
      subAreaComboBox.Items.Add(area); 
     } 

这包括你不要有你的列表中的结尾逗号(因为你有此刻你的第一个位置,如果是这样的,你必须扩展代码才能删除它

+0

感谢您的良好开端!两个问题:我不确定+ locationID +应该是什么?我的代码不会用它编译。其次,如果你有“//写入子区域目标”,我想直接将值放入子区域框中,那么我需要做什么才能将其实现到子区域框中? – 2012-08-06 12:47:18

+0

locationID是一个变量,它保存与头节点中的id-attribute相同的值。让我们来澄清你想要的,然后编辑我的代码:你有两个文本框,当你在第一个输入“04”时,你想在第二个输入“A,B,C”? – Jan 2012-08-06 12:51:37

+0

这是正确的!他们将选择一个位置(从下拉框中)并根据位置选择列表中的选项将出现在与该位置相关的子区域下拉列表中。因此,如果他们在地点下拉框中选择“04”,子区域“A,B,C”只会出现在子区域下拉框中 – 2012-08-06 12:54:41

0

这里有一些公平的步骤,我使用XDocument.Load来加载文档,然后你需要将它绑定到UI,如果使用WPF,一些ItemsControl的衍生物来输入值,但是你提到TextBoxes的事实是值得警惕的,在ASP.NET中同样如此,因为所有的控件都需要重新创建,给定相同的ID并重新添加到页面中同时加载ViewState以便交流塞住他们。

当你绑定/重新创建它们时,关键是确保所有的ID/Name属性保持不变。

我个人的经验一直是WPF中的动态控件很小,但在ASP.NET中很难正常工作。

相关问题