Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
this is my xml files dropdown
XML
<?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::
HTML
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" datafile=&
</asp:XmlDataSource>

and below is code behind code
C#
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();
Posted
v5
Comments
So what is the issue?
JoCodes 3-Dec-13 3:41am    
Is it not binding?

1 solution

Basically You are missing DataValueField and DataTextField for more details see DropDownList Properties
To Bind a Dropdown
try Something Like that
C#
DropDownList1.DataValueField = "value";
DropDownList1.DataTextField = "text";
DropDownList1.DataSource = query;
DropDownList1.DataBind();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900