2011-10-27 33 views
1

我在我的網站上很多地方使用的網頁上有三個CascadingDropDown s。他們從數據庫中加載了國家,地區和地區名稱。我已經成功綁定了數據並將它們設置爲級聯。但是當用戶在我的網站上註冊時,我想在三個CascadingDropDown之間預先選擇三個值。從CascadingDropDown中的數據庫預填充數據

註冊之後,我還需要在管理區域看到他們的數據,我還需要預先選擇三個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"; 

Page_Load帶有回發檢查。

請參見:SelectedValue Property

+0

ChrisBint,我嘗試過這一點,但是這並不在下拉列表... – Ratul

-1

我得到了答案。 ChrisBint,SelectedValue屬性不應該是DropDown本身。它應該是CascadingDropDown。所以,現在我的代碼是:

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

而作爲ChrisBint說,我寫這篇文章的代碼外,如果塊(Page.IsPostBack!)。

+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()