【问题标题】:fetch data in dropdownlsit using linq使用 linq 在下拉列表中获取数据
【发布时间】:2012-04-30 19:17:43
【问题描述】:

我是 ASP.NET 的新手,

我正在制作国家,州下拉列表。

例如:对于特定国家/地区,我将从 XML 文件中读取该国家/地区的状态。

我无法在下拉列表中获取所需国家/地区的状态...

这是我在XMLFile.xml中的代码sn-p

 <?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();     
    }
}

错误: 对象引用未设置为对象的实例。

提前致谢!

【问题讨论】:

  • 你在哪一行得到错误
  • 我的 linq query 给了我空值.. 你能检查一下...
  • a) 您的 xml 中没有名称为 "country_text" 的元素 b) 您的州不在 country 元素内.. c) "state" 没有价值,但 "text" 在州内有价值..请在使用之前正确修改您的xml
  • 您的 xml 文档并不暗示一个国家与其下属的州之间的关系。也许像&lt;country name="country1"&gt;&lt;state name="state1"/&gt;&lt;state name="state2"/&gt;&lt;/country&gt; 这样的结构更适合
  • 我按照你的建议编写了我的 xnl 文件,并尝试了这个查询,但我仍然没有得到 o/p: var query = from user in doc.Descendants("state") where st == user .Element("state").Value select user.NextNode;

标签: asp.net xml linq drop-down-menu data-binding


【解决方案1】:

只有一种方法可以填充下拉列表。代码将更容易理解和维护。如果没有选定的国家/地区(在第一页加载时),则将 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);
        }

【讨论】:

  • .Element(selectedConty).Element("state") 这将返回 null ,对于 country 元素,没有名称为 state 的元素
  • selectedCountry 是我在方法中引入的一个参数。 (阅读代码)@Flowerking:谢谢我已更正。
【解决方案2】:
  XDocument main = XDocument.Load(@"data.xml"); 
        var query = from state in main.Descendants("countrys").Elements("country")
                    where st == state.Value
                    select state.NextNode; 

【讨论】:

    【解决方案3】:

    好的,这是解决方案: xml 中的前几处更改,状态元素中的属性“value1”应该是所有的值。所以新的 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 来填充 Country - 我在这里也使用 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中的国家)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-08
      • 1970-01-01
      相关资源
      最近更新 更多