1

私は ASP.NET を初めて使用します。

国、州のドロップダウンリストを作成しています。

例: 特定の国について、キャッシュ ファイルからその国の州を読み取ります。

LOGIC :ユーザーが初めて訪問したとき、その時点でキャッシュは空になるため、対応する州が からフェッチされXMLFile、その州がキャッシュに挿入されます。次にユーザーが同じ国を訪問するときは、州は からではなくキャッシュから取得する必要がありますXMLFile

ドロップダウンリストで必要な国の州を取得できません...

これが私のコードスニペットです。

protected void DropDownListCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
           string  SelectedCountry = DropDownListCountry.SelectedItem.Text;

           XDocument doc = XDocument.Load(Server.MapPath("XMLFile.xml"));

           var query = from country in doc.Descendants("country")
                       where country.Attribute("name").Value == SelectedCountry
                       select new
                       {
                           states = from state in country.Elements("state")
                                    select new{
                                        Name = state.Attribute("name").Value
                                    }
                       };

           DataTable dt = new DataTable();
           dt.Columns.Add(new DataColumn("Name"));

            if (Cache["key"]==null)
            {
                foreach (var st in query)
                {
                    foreach (var StateName in st.states)
                    {
                        DataRow dr = dt.NewRow();
                        dr["Name"] = StateName.Name;
                        dt.Rows.Add(dr);
                        Cache.Insert("key", dr);

                    }
                }
                DropDownListState.DataSource = dt;
                DropDownListState.DataTextField = "Name";
                DropDownListState.DataBind();


            }
            else
            {
                      object obj = Cache["key"];
                      dt.Rows.Add(obj);

                DropDownListState.DataSource = dt;
                DropDownListState.DataTextField = "Name";
                DropDownListState.DataBind();
            }        
    }

前もって感謝します !!

4

2 に答える 2

2

このコードには、データと UI の処理方法、キャッシュの使用方法、およびバグ、キャッシュ キーはオブジェクトごとに一意である必要があるなど、いくつかの問題があります。

それでは、コードを少しリファクタリングして、国名ごとの州を含む DataTable を返すメソッドを作成しましょう。状態が既にキャッシュにある場合は、キャッシュ内のその DataTable が返されます。

private static DataTable GetStates(string countryName)
{
   DataTable dt = HttpContext.Current.Cache[countryName] as DataTable;
   if (dt != null)
       return dt;

   // the datatable is not in cache, let's create it then and add it to cache, next call will pick it from cache and won't read and parse the xml again
   var doc = XDocument.Load(HttpContext.Current.Server.MapPath("XMLFile.xml"));
   var query = from country in doc.Descendants("country")
                 where country.Attribute("name").Value == countryName
                 select new
                 {
                     states = from state in country.Elements("state")
                              select new
                              {
                                  Name = state.Attribute("name").Value
                              }
                 };

  dt = new DataTable();
  dt.Columns.Add(new DataColumn("Name"));

 foreach (var st in query)
            {
                foreach (var StateName in st.states)
                {
                    DataRow dr = dt.NewRow();
                    dr["Name"] = StateName.Name;
                    dt.Rows.Add(dr);

                }
            }
 // add the data table in the cache (not a row and don't use the same key)
 HttpContext.Current.Cache.Insert(countryName, dt);   
 // return this table
 return dt;
}

今度はイベントハンドラー

protected void DropDownListCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
       var countryName = DropDownListCountry.SelectedItem.Text;
       var dt = GetStates(countryName);

       DropDownListState.DataSource = dt;
       DropDownListState.DataTextField = "Name"; // you can set it in markup also
       DropDownListState.DataBind();
    }

キャッシュはすべてのユーザーで共有されるため、これが意図されている場合は使用してもかまいません。

于 2012-04-20T11:26:17.563 に答える
0

このようなものが役立つかもしれません:

private DataTable PopulateCountryList()
{
  DataTable dt = (DataTable)Cache["countries"];

  if (dt == null)
  {
    dt.Columns.Add(new DataColumn("Name"));

    XDocument doc = XDocument.Load(Server.MapPath("XMLFile.xml"));

    var query = from country in doc.Descendants("country")
    where country.Attribute("name").Value == SelectedCountry
    select new
    {
        states = from state in country.Elements("state")
        select new {
        Name = state.Attribute("name").Value
        }
    };

    foreach (var st in query)
    {
        foreach (var StateName in st.states)
        {
          DataRow dr = dt.NewRow();
          dr["Name"] = StateName.Name;
          dt.Rows.Add(dr);    
        }
    }

    Cache.Insert("countries", dt, null, DateTime.Today.AddDays(1), TimeSpan.Zero);
  }

  return = dt;
}

ドロップダウンにデータを入力する場合:

DropDownListState.DataSource = PopulateCountryList();
DropDownListState.DataTextField = "Name"; 
DropDownListState.DataBind();
于 2012-04-20T11:28:29.233 に答える