1

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

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

提前致谢 !!

4

3 回答 3

2

好的,这里是解决方案:首先在 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 中的国家/地区)

于 2012-04-19T11:42:36.807 回答
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);
        }
于 2012-04-19T09:57:29.547 回答
0
  XDocument main = XDocument.Load(@"data.xml"); 
        var query = from state in main.Descendants("countrys").Elements("country")
                    where st == state.Value
                    select state.NextNode; 
于 2012-04-19T10:21:29.193 回答