0

ASP.NETは初めてですが、

国、州のドロップダウンリストを作成しています。

例:特定の国については、XMLファイルからその国の状態を読み取ります。

これが私のコードスニペットですXMLFile.xml

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

  <country>India</country>
  <state>
    <text>Maharashtra</text>
    <text>Kashmir</text>
    <text>Goa</text>   
  </state>

  <country>Sri Lanka</country>
  <state>
    <text>Kanady</text>
    <text>Colombo</text>
    <text>Galle</text> 
  </state>

  <country>Australia</country>
  <state>
    <text>Sydney</text>
    <text>Perth</text>
    <text>Melbourne</text>
  </state>

  <country>South Africa</country>
  <state>
    <text>Capetown</text>
    <text>Johanusburg</text>
    <text>Durban</text>
  </state>
</countrys>

とコードCountry.aspx.cs

   public partial class Country : System.Web.UI.Page
   {
       protected void Page_Load(object sender, EventArgs e)
       {

            if (!IsPostBack)
            {
                LoadDropdown();
            }
     }

    protected void LoadDropdown()
    {
            DataSet ds = new DataSet();
            ds.ReadXml (Server.MapPath("XMLFile.xml"));

            DropDownListCountry.DataTextField = "country";

            DropDownListCountry.DataSource = ds;
            DropDownListCountry.DataBind();
            DropDownListCountry.Items.Insert(0,new ListItem(" Select ","0"));
        }
     }

    protected void DropDownListCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
            string  st = (DropDownListCountry.SelectedIndex).ToString();

             XDocument main = XDocument.Load(@"XMLFile.xml");

        var query = from user in main.Descendants("country_text")
                where st == user.Element("state").Value
                select user;

        DropDownListState.DataSource = query;
        DropDownListState.DataBind();     
    }
}

エラー: DataBinding:'System.Data.DataRowView'には、'country'という名前のプロパティが含まれていません。

4

1 に答える 1

1

それをcountry_textにバインドします

DropDownListCountry.DataTextField = "country_text";

データセットには3つのテーブルがあります。国、州、テキスト。国の値を保持するデータセットのフィールドはcountry_textであり、これにバインドする必要があります

于 2012-04-19T08:56:52.283 に答える