0
私はASP.NETを初めて使用しています。XMLを使用したドロップダウンリストのバインド
私は国、州のドロップダウンリストを作成しています。
例:特定の国では、その国の州をXMLファイルから読み込みます。データバインド: 'System.Data.DataRowView' 名前のプロパティが含まれていませんここ
は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();
}
}
ERRORで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>
、コードの私のコードスニペットです'国'。あなたは、データセット内の3つのテーブルを持っている
DropDownListCountry.DataTextField = "country_text";
をcountry_textにバインド
ですが、このエラーが発生したときに「C:\ Program Files \ Common Files \ Microsoft Shared \ DevServer \ 10.0 \ XMLFile.xml '。 ' – Krunal
selectedindexchangedイベントで、Server.MapPath(" state.xml ")を使用していないためです。今までXMLFileをロードしているところでServer.MapPathを使用してください – Habib
私はこれを試しました、 'DataSet ds = new DataSet(); ds.ReadXml(Server.MapPath( "XMLFile.xml")); var query =ユーザーのds.Descendants( "country_text") st == user.Element( "state")。値 ユーザーを選択してください。 DropDownListState.DataSource = query; DropDownListState.DataBind(); ' – Krunal