2011-10-27 30 views
1

私は自分のウェブサイトの多くの場所で使用しているウェブページに3つのCascadingDropDownを持っています。彼らは、データベースからの国、地区、および地域の名前を読み込んでいます。私は正常にそれらとデータをバインドし、それらをカスケードするように設定しました。しかし、ユーザーが自分のサイトに登録したときに3つの値を事前に選択したいのですが、CascadingDropDownです。CascadingDropDownのデータベースにあらかじめ入力されたデータ

登録後、ユーザーが選択した値を持つ3つのCascadingDropDownを事前に選択する必要がある管理領域でそのデータを表示する必要があります。しかし、私はこれを行う方法を理解することはできません。

私のWebサービスコード:

namespace ZetaSolutions.WebProjects.Web.Modules 
{ 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.Web.Script.Services.ScriptService()] 
    public class PlaceSelection : System.Web.Services.WebService 
    { 
     [WebMethod] 
     public CascadingDropDownNameValue[] BindCountryDropDown(string knownCategoryValues, string category) 
     { 
      SqlConnection conCountry = new SqlConnection(ZetaConfig.ConnectionString); 
      conCountry.Open(); 

      SqlCommand cmdCountry = new SqlCommand("SELECT * FROM Country ORDER BY Name", conCountry); 
      SqlDataAdapter daCountry = new SqlDataAdapter(cmdCountry); 
      cmdCountry.ExecuteNonQuery(); 

      DataSet dsCountry = new DataSet(); 
      daCountry.Fill(dsCountry); 
      conCountry.Close(); 

      List<CascadingDropDownNameValue> countryDetails = new List<CascadingDropDownNameValue>(); 

      foreach (DataRow dtRow in dsCountry.Tables[0].Rows) 
      { 
       string countryId = dtRow["CountryID"].ToString(); 
       string countryName = dtRow["Name"].ToString(); 

       countryDetails.Add(new CascadingDropDownNameValue(countryName, countryId)); 
      } 

      return countryDetails.ToArray(); 
     } 

     [WebMethod] 
     public CascadingDropDownNameValue[] BindDistrictDropDown(string knownCategoryValues, string category) 
     { 
      int countryId; 
      StringDictionary countryDetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); 
      countryId = Convert.ToInt32(countryDetails["Country"]); 

      SqlConnection conDistrict = new SqlConnection(ZetaConfig.ConnectionString); 
      conDistrict.Open(); 
      SqlCommand cmdDistrict = new SqlCommand("SELECT * FROM District WHERE [email protected] ORDER BY Name", conDistrict); 
      cmdDistrict.Parameters.AddWithValue("@CountryID", countryId); 
      cmdDistrict.ExecuteNonQuery(); 

      SqlDataAdapter daDistrict = new SqlDataAdapter(cmdDistrict); 
      DataSet dsDistrict = new DataSet(); 
      daDistrict.Fill(dsDistrict); 
      conDistrict.Close(); 

      List<CascadingDropDownNameValue> districtDetails = new List<CascadingDropDownNameValue>(); 

      foreach (DataRow dtDistrictRow in dsDistrict.Tables[0].Rows) 
      { 
       string districtId = dtDistrictRow["DistrictID"].ToString(); 
       string districtName = dtDistrictRow["Name"].ToString(); 
       districtDetails.Add(new CascadingDropDownNameValue(districtName, districtId)); 
      } 

      return districtDetails.ToArray(); 
     } 

     [WebMethod] 
     public CascadingDropDownNameValue[] BindAreaDropDown(string knownCategoryValues, string category) 
     { 
      int districtId; 
      StringDictionary districtDetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); 
      districtId = Convert.ToInt32(districtDetails["District"]); 

      SqlConnection conArea = new SqlConnection(ZetaConfig.ConnectionString); 
      conArea.Open(); 
      SqlCommand cmdArea = new SqlCommand("SELECT * FROM Area WHERE [email protected] ORDER BY Name", conArea); 
      cmdArea.Parameters.AddWithValue("@DistrictID ", districtId); 
      cmdArea.ExecuteNonQuery(); 

      SqlDataAdapter daArea = new SqlDataAdapter(cmdArea); 
      DataSet dsArea = new DataSet(); 
      daArea.Fill(dsArea); 
      conArea.Close(); 

      List<CascadingDropDownNameValue> areaDetails = new List<CascadingDropDownNameValue>(); 

      foreach (DataRow dtAreaRow in dsArea.Tables[0].Rows) 
      { 
       string areaId = dtAreaRow["AreaID"].ToString(); 
       string areaName = dtAreaRow["Name"].ToString(); 
       areaDetails.Add(new CascadingDropDownNameValue(areaName, areaId)); 
      } 

      return areaDetails.ToArray(); 
     } 
    } 
} 

私のaspxコード:

<table> 
    <tr> 
     <td> 
      Country: 
     </td> 
     <td> 
      <asp:DropDownList ID="ddlCountry" runat="server"></asp:DropDownList> 
      <ajaxToolkit:CascadingDropDown ID="CountryCascading" runat="server" Category="Country" TargetControlID="ddlCountry" LoadingText="Loading Countries..." PromptText="Select Country" ServiceMethod="BindCountryDropDown" ServicePath="PlaceSelection.asmx"> 
      </ajaxToolkit:CascadingDropDown> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      District: 
     </td> 
     <td> 
      <asp:DropDownList ID="ddlDistrict" runat="server"></asp:DropDownList> 
      <ajaxToolkit:CascadingDropDown ID="DistrictCascading" runat="server" Category="District" TargetControlID="ddlDistrict" ParentControlID="ddlCountry" LoadingText="Loading Districts..." PromptText="Select District" ServiceMethod="BindDistrictDropDown" ServicePath="PlaceSelection.asmx"> 
      </ajaxToolkit:CascadingDropDown> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      Area: 
     </td> 
     <td> 
      <asp:DropDownList ID="ddlArea" runat="server"></asp:DropDownList> 
      <ajaxToolkit:CascadingDropDown ID="AreaCascading" runat="server" Category="Area" TargetControlID="ddlArea" ParentControlID="ddlDistrict" LoadingText="Loading Areas..." PromptText="select Areas" ServiceMethod="BindAreaDropDown" ServicePath="PlaceSelection.asmx"> 
      </ajaxToolkit:CascadingDropDown> 
     </td> 
    </tr> 
</table> 

誰がどのように私は私の問題を解決することができますを教えていただけますか?緊急です。前もって感謝します。

答えて

1

「管理者」システムから選択したい値を読み、それを使用する必要があります。

DropDownList.SelectedValue = "value"; 

(ポストバックチェックあり)

参照:SelectedValue Property

+0

ChrisBint、 – Ratul

-1

は、私は答えを得ました。 ChrisBint、SelectedValueプロパティはDropDown自体ではありません。それはCascadingDropDownでなければなりません。だから今、私のコードは次のとおりです。(!Page.IsPostBack)

CountryCascading.SelectedValue = countryId.ToString(); 
DistrictCascading.SelectedValue = districtId.ToString(); 
AreaCascading.SelectedValue = areaId.ToString(); 

そしてChrisBintが言ったように、私があれば外にこのコードを記述するブロック。

+0

...私は前にこれを試してみましたが、これはドロップダウンリストで値を選択していないが「DropDownListの」カット&ペーストであることを意図していなかった、それは単に「」コントロールへの参照ましたおよび関連するプロパティ。私は何をすべきかを説明することを望んでいたと思うでしょう。また、ポストバックチェックの外でこれを行うと、ページが読み込まれて上書きされるたびに更新されます。私はこれらの理由の両方でこれを打ち切った。 – ChrisBint

0

ありがとう、このコードは私のために働いています。

CascadingDropdownID1.SelectedValue = dsdataset.Table(0).Rows(0)(0).ToString() 
    CascadingDropdownID2.SelectedValue = dsdataset.Table(0).Rows(0)(1).ToString() 
    CascadingDropdownID3.SelectedValue = dsdataset.Table(0).Rows(0)(2).ToString() 
関連する問題