Reputation: 117
I am binding XML values to a DropDownList. Below is my code:
protected void LoadDropdown()
{
DataSet ds = new DataSet();
ds.ReadXml (Server.MapPath(@"XMLFile1.xml"));
DropDownList1.DataTextField = "country_Id";
DropDownList1.DataSource = ds;
DropDownList1.DataBind();
DropDownList1.Items.Insert(0,new ListItem(" Select ","0"));
}
I want to get country names in the DropDownList, but I am getting id values like 0,1,2,3. What am I doing wrong?
Upvotes: 1
Views: 524
Reputation: 117
I found the solution for my problem.
protected void LoadDropdown()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(@"XMLFile1.xml"));
DropDownList1.DataTextField = "name";
DropDownList1.DataSource = ds;
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem(" Select ", "0"));
}
Upvotes: 0
Reputation: 2095
if you xml looks like this
<?xml version="1.0" encoding="utf-8" ?>
<Items>
<Item ddlValue="1" ddlText="YourlistItem1" />
<Item ddlValue="2" ddlText="YourlistItem2" />
<Item ddlValue="3" ddlText="YourlistItem3" />
</Items>
your code behind for your dropdownlist should be
protected void Page_Load(object sender, EventArgs e)
{
DataSet ddlDataSource = new DataSet();
ddlDataSource.ReadXml(MapPath("XmlFile.xml"));
DropDownList1.DataSource = ddlDataSource;
DropDownList1.DataBind();
}
Hope this helps.
Upvotes: 0
Reputation: 85056
Try specifying something else for the DataTextField:
DropDownList1.DataTextField = "country_Name"; //This value depends on what your XML structure is.
DropDownList1.DataValueField = "country_Id";
Upvotes: 1