ASP.NETは初めてですが、
国、州のドロップダウンリストを作成しています。
例:特定の国については、XMLファイルからその国の状態を読み取ります。
ドロップダウンリストで必要な国の州を取得できません...
これが私のコードスニペットですXMLFile.xml
<?xml version="1.0" encoding="utf-8" ?>
<countrys>
<country name="India">
<state value1="Maharashtra"></state>
<state value2="Kashmir"></state>
<state value3="Goa"></state>
</country>
<country name="Sri Lanka">
<state value1="Kanady"></state>
<state value2="Colombo"></state>
<state value3="Galle"></state>
</country>
<country name="Australia">
<state valu1e="Sydney"></state>
<state value2="Perth"></state>
<state value3="Melbourne"></state>
</country>
<country name="South Africa">
<state value1="Capetown"></state>
<state value2="Johanusburg"></state>
<state value3="Durban"></state>
</country>
</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_text";
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 state in doc.Descendants("countrys").Elements("country")
where st == state.Value
select state.NextNode;
DropDownListState.DataSource = query;
DropDownListState.DataBind();
}
}
エラー: オブジェクト参照がオブジェクトのインスタンスに設定されていません。
前もって感謝します !!