如何通过xml文件填充下拉列表 [英] How to fill dropdown by xml file
本文介绍了如何通过xml文件填充下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的xml文件下拉菜单
this is my xml files dropdown
<?xml version="1.0" encoding="utf-8" ?>
<dropdowns>
<dropdown name="DropDownLoc">
<menu text="Select" value="-1" />
<menu text="North" value="1200" />
<menu text="South" value="1400" />
</dropdown>
<dropdown name="DropDownEsp">
<menu text="Select" value="-1" />
<menu text="Est" value="7" />
<menu text="Ovest" value="9" />
</dropdown>
</dropdowns>
现在这是我的设计页面::
now this is my design page::
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" datafile=&
</asp:XmlDataSource>
及以下是代码背后的代码
and below is code behind code
string path = Server.MapPath("~/XMLFile3.xml");
string whichDropDown = "DropDownEsp";
var query =
from dropDown in XDocument.Load(path).Descendants("dropdown")
where dropDown.Attribute("name").Value == whichDropDown
from name in dropDown.Descendants("name")
let text = name.Attribute("text").value
let value = name.Attribute("value").value
select new ListItem(text, value);
// Now we data bind the query result to the control
DropDownList1.DataSource = query;
DropDownList1.DataBind();
推荐答案
基本上您缺少DataValueField和DataTextField以获取更多详细信息,请参阅DropDownList Properties
绑定下拉菜单
尝试类似的东西
Basically You are missing DataValueField and DataTextField for more details see DropDownList Properties
To Bind a Dropdown
try Something Like that
DropDownList1.DataValueField = "value";
DropDownList1.DataTextField = "text";
DropDownList1.DataSource = query;
DropDownList1.DataBind();
这篇关于如何通过xml文件填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文