1

我将 XML 值绑定到 DropDownList。下面是我的代码:

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"));
}     

我想在 DropDownList 中获取国家/地区名称,但我得到了 0、1、2、3 之类的 id 值。我究竟做错了什么?

4

3 回答 3

1

尝试为 DataTextField 指定其他内容:

DropDownList1.DataTextField = "country_Name"; //This value depends on what your XML structure is.
DropDownList1.DataValueField = "country_Id";
于 2012-06-04T18:09:47.723 回答
0

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.

于 2012-06-04T18:21:01.737 回答
0

我找到了解决我的问题的方法。

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"));
    }
于 2012-06-15T11:04:07.390 回答