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

xml が次のようになっている場合

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

<Items>
<Item ddlValue="1" ddlText="YourlistItem1" />
<Item ddlValue="2" ddlText="YourlistItem2" />
<Item ddlValue="3" ddlText="YourlistItem3" />
</Items>

ドロップダウンリストのコードビハインドは

protected void Page_Load(object sender, EventArgs e)
{
    DataSet ddlDataSource = new DataSet(); 
    ddlDataSource.ReadXml(MapPath("XmlFile.xml"));
    DropDownList1.DataSource = ddlDataSource;
    DropDownList1.DataBind();
} 

お役に立てれば。

于 2012-06-04T18:21:01.737 に答える