我是ASP.NET新手,使用linq获取dropdownlsit中的数据
我正在制作国家,州下拉列表。
例如:对于特定的国家,我将从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();
}
}
错误:未将对象引用设置为ins物体的状态。
在此先感谢!
哪条线会出错 – freebird 2012-04-19 09:36:44
my linq'quer y'给我空值..你可以plz检查... – Krunal 2012-04-19 09:38:43
a)在你的xml中没有元素名称为“country_text”b)你的状态不在country元素内c)''state “'没有价值,但''文字''里面的状态有价值..请在使用之前正确修改您的xml – Flowerking 2012-04-19 09:39:01