2012-04-19 64 views
1

我是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物体的状态。

在此先感谢!

+0

哪条线会出错 – freebird 2012-04-19 09:36:44

+0

my linq'quer y'给我空值..你可以plz检查... – Krunal 2012-04-19 09:38:43

+0

a)在你的xml中没有元素名称为“country_text”b)你的状态不在country元素内c)''state “'没有价值,但''文字''里面的状态有价值..请在使用之前正确修改您的xml – Flowerking 2012-04-19 09:39:01

回答

2

确定这里是解决方案:在XML 一是一些变化,在状态元素属性“值1”,应该是所有的价值。因此,新的XML是:

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

    <country name="India"> 
    <state value="Maharashtra"></state> 
    <state value="Kashmir"></state> 
    <state value="Goa"></state> 
    </country> 

    <country name="Sri Lanka"> 
    <state value="Kanady"></state> 
    <state value="Colombo"></state> 
    <state value="Galle"></state> 
    </country> 

    <country name="Australia"> 
    <state value="Sydney"></state> 
    <state value="Perth"></state> 
    <state value="Melbourne"></state> 
    </country> 

    <country name="South Africa"> 
    <state value="Capetown"></state> 
    <state value="Johanusburg"></state> 
    <state value="Durban"></state> 
    </country> 

</countrys> 

现在来ASPX页面:你应该有两个下拉列表具有的AutoPostBack设置为true

<asp:DropDownList ID="DropDownListCountry" runat="server" AutoPostBack="true" 
    onselectedindexchanged="DropDownListCountry_SelectedIndexChanged"> 
</asp:DropDownList> 
<asp:DropDownList ID="DropDownListState" runat="server" AutoPostBack="true" 
    onselectedindexchanged="DropDownListState_SelectedIndexChanged"> 
</asp:DropDownList> 

现在,在后面的代码:
呼叫LoadCountryDropDown填充国家 - 我在这里使用LINQ to XML代替数据集

protected void Page_Load(object sender, EventArgs e) 
     { 

      if (!IsPostBack) 
      { 
       LoadCountryDropDown(); 
      } 

     } 
     void LoadCountryDropDown() 
     { 
      XDocument doc = XDocument.Load(Server.MapPath("test.xml")); 

      DropDownListCountry.DataSource = from t in doc.Descendants("countrys").Elements("country") 
              select new 
              { 
               Name = t.Attribute("name").Value 
              }; 

      DropDownListCountry.DataTextField = "Name"; 
      DropDownListCountry.DataValueField = "Name"; 
      DropDownListCountry.DataBind(); 
      DropDownListCountry.Items.Insert(0, new ListItem(" Select ", "0")); 
     } 

LoadStateDropDown()方法来填充状态下拉上国的若干指数变化情况下

private void LoadStateDropDown(string p) 
    { 
     XDocument doc = XDocument.Load(Server.MapPath("test.xml")); 

     var statequery = from t in doc.Descendants("countrys").Elements("country") 
             where t.Attribute("name").Value.Equals(p) 
             select new 
             { 
              State = t.Elements("state").Attributes("value").ToList() 
             }; 

     DropDownListState.DataSource = statequery.First().State; 
     DropDownListState.DataTextField = "value"; 
     DropDownListState.DataValueField = "value"; 
     DropDownListState.DataBind(); 
     DropDownListState.Items.Insert(0, new ListItem(" Select ", "0")); 
    } 

滴在你到底有没有事件处理程序下拉列表

protected void DropDownListCountry_SelectedIndexChanged(object sender, EventArgs e) 
     { 

      LoadStateDropDown(DropDownListCountry.SelectedValue); 
     } 
    protected void DropDownListState_SelectedIndexChanged(object sender, EventArgs e) 
     { 

     } 


(请用xml将国家重新命名为国家)

0

只需要一种方法来填充下拉菜单。代码将更容易理解和维护。如果没有选定的国家/地区(首次加载页面),则将null作为参数传递。 根据上面的xml文件,你似乎写了错误的字符串来选择状态。 selectedindex出现错误。使用选定的值。希望这有助于:

 protected void Page_Load(object sender, EventArgs e) 
     { 

      if (!IsPostBack) 
      { 
       LoadDropdown(null); 
      } 
    } 
    protected void LoadDropdown(string selectedCountry) 
     { 
       XDocument main = XDocument.Load(@"XMLFile.xml"); 
       if(selectedCountry != null) 
       { 
         DropDownListCountry.DataSource = from state in main.Element("countries").Element(selectedConty).Elements("state") 
         where state.Value = selectedCountry 
select state.Value; 
       } 
       else 
       { 
         DropDownListCountry.DataSource = null; 
       } 
       DropDownListCountry.DataBind(); 
       DropDownListCountry.Items.Insert(0,new ListItem(" Select ","0")); 
      } 
     } 

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

       LoadDropdown(st); 
     } 
+0

什么是在条件中提到的'selectedConty' .. – Krunal 2012-04-19 10:07:40

+0

'.Element(selectedConty).Element(“state”)'这将返回'null',没有名称为'state'的元素作为'country'元素 – Flowerking 2012-04-19 10:13:09

+0

selectedCountry是我已经介绍给该方法的一个参数。 (阅读代码) @Flowerking:谢谢我已纠正。 – mortb 2012-04-19 10:45:42

0
XDocument main = XDocument.Load(@"data.xml"); 
     var query = from state in main.Descendants("countrys").Elements("country") 
        where st == state.Value 
        select state.NextNode; 
+0

在下拉列表中没有收到任何内容.. – Krunal 2012-04-19 10:40:24