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の最初のいくつかの変更では、state要素の属性'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に設定された2つのドロップダウンリストが必要です。

<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を呼び出して国にデータを入力します-ここでもデータセットの代わりにLINQtoXMLを使用しています

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
  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 に答える
0

ドロップダウンを埋める方法は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);
        }
于 2012-04-19T09:57:29.547 に答える