2013-03-12 83 views
0

我有2個級聯下拉菜單,1是父級,一旦您在第一個/父級下拉菜單中選擇一個值,則會填充其他/子級。根據父值設置選定值

第一個在頁面加載時填充,我可以根據上一頁保存的數據庫值爲此設置選定的值。

對於第二個級聯下拉菜單,當我在頁面加載時(基於數據庫值)設置父級ID的選定值時,它會爲我填充。

但是由於第二個/下拉菜單沒有設置/加載值直到頁面加載,我無法設置選定的值(我在Page_Load_Complete上嘗試過,但它在那裏也不起作用)。

我需要知道如何在父級下拉菜單中設置所選值(此工作正常),以基於第一個下拉菜單中的值填充第二個下拉菜單。

這是我的aspx頁面的代碼。我可以設置所選的第一個值,它填充第二個選擇框(但我不能設置所選擇的價值,因爲它不是在我設定的第一個選定值的時間填充。

aspx頁面

<asp:Label ID="lblAffPartCat" Text="<%$ Resources:share,lblAffPartCat %>" runat="server"></asp:Label> 
<asp:DropDownList ID="ddlPartCat" runat="server"></asp:DropDownList> 

<ajaxToolkit:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="ddlPartCat" 
Category="BasePart" PromptText="<%$ Resources:share,lblSelPartCat %>" LoadingText="[Loading Part Cat...]" 
ServicePath="PAIntExtPart.asmx" ServiceMethod="BindPartCat" 
ContextKey="" UseContextKey="True"/> 

<asp:Label ID="lblAffBasePart" Text="<%$ Resources:share,lblAffBasePart %>" runat="server"></asp:Label> 

<asp:DropDownList ID="ddlBasePart" runat="server" ></asp:DropDownList> 

<ajaxToolkit:CascadingDropDown ID="ddlBasePart_CascadingDropDown" runat="server" Category="BasePart" 
TargetControlID="ddlBasePart" ParentControlID= "ddlPartCat" PromptText="<%$ Resources:share,lblSelBasePart %>" 
LoadingText="Loading Base Parts.." 
ServicePath="PAIntExtPart.asmx" 
ServiceMethod="BindBasePart" 
ContextKey="" UseContextKey="True" /> 

asmx.cs頁面填充下拉列表:

using System; 
using System.Collections.Generic; 
using System.Web.Services; 
using System.Data; 
using System.Collections.Specialized; 
using AjaxControlToolkit; 
using Hotline.DataAccess; 

/// <summary> 
    /// Summary description for PAIntExtPart 
    /// </summary> 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService()] 
    public class PAIntExtPart : System.Web.Services.WebService 
    { 
     string _SiteLocation = MiscFunctions.getCurrentSiteLocation(); 

     /// <summary> 
     /// WebMethod to Populate Part Category Dropdown 
     /// </summary> 
     [WebMethod] 
     public CascadingDropDownNameValue[] BindPartCat(string knownCategoryValues, string category, string contextKey) 
     { 
      DataTable dsPartCat = null; 

      // string passed for contextKey is FormType and Language split by ":" 
      string[] arrcontextKey = contextKey.Split(':'); 

      string FormType = arrcontextKey[0].ToString(); 
      int LanguageID = Int32.Parse(arrcontextKey[1].ToString()); 
      string PartCatValue = arrcontextKey[2].ToString(); 

      try 
      {     
       dsPartCat = HarDB.getPartCat(_SiteLocation, LanguageID, FormType); 

       //create list and add items in it by looping through dataset table 
       List<CascadingDropDownNameValue> PartCatdetails = new List<CascadingDropDownNameValue>(); 
       foreach (DataRow dtrow in dsPartCat.Rows) 
       { 
        string PartCatID = dtrow["PartCatID"].ToString(); 
        string PartCat = dtrow["PartCat"].ToString(); 
        PartCatdetails.Add(new CascadingDropDownNameValue(PartCat, PartCatID)); 

       } 

       if (PartCatValue.Trim() != "") 
       {      
        //SelectedValue = PartCatValue; 
       } 

       return PartCatdetails.ToArray(); 

      } 
      catch (Exception ex) 
      { 
       Server.Transfer("Errorpage.aspx?function=getAttachInfo+Error=" + Server.UrlEncode(ex.Message)); 
       return null; 
      } 

     } 

     /// <summary> 
     /// WebMethod to Populate Base Part Dropdown 
     /// </summary> 
     [WebMethod] 
     public CascadingDropDownNameValue[] BindBasePart(string knownCategoryValues, string category, string contextKey) 
     { 
      string PartCatID; 
      //int LanguageID = Int32.Parse(contextKey); 

      string[] arrcontextKey = contextKey.Split(':'); 

      string FormType = arrcontextKey[0].ToString(); 
      int LanguageID = Int32.Parse(arrcontextKey[1].ToString()); 
      string BasePartValue = arrcontextKey[2].ToString(); 

      //This method will return a StringDictionary containing the name/value pairs of the currently selected values 
      StringDictionary PartCatdetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); 

      PartCatID = PartCatdetails["BasePart"]; 


      DataTable dsBasePart = null; 
      try 
      { 
       dsBasePart = HarDB.getBasePart(_SiteLocation, LanguageID, PartCatID, FormType); 

       //create list and add items in it by looping through dataset table 
       List<CascadingDropDownNameValue> BasePartdetails = new List<CascadingDropDownNameValue>(); 
       foreach (DataRow dtrow in dsBasePart.Rows) 
       { 
        string BasePartID = dtrow["BasePartNumID"].ToString(); 
        string BasePart = dtrow["BasePartNum"].ToString(); 
        BasePartdetails.Add(new CascadingDropDownNameValue(BasePart, BasePartID)); 
       } 

       if (BasePartValue.Trim() != "") 
       { 
        //SelectedValue = PartCatValue; 
       } 

       return BasePartdetails.ToArray(); 

      } 
      catch (Exception ex) 
      { 
       Server.Transfer("Errorpage.aspx?function=getAttachInfo+Error=" + Server.UrlEncode(ex.Message)); 
       return null; 
      } 

     } 

    } 
+0

我已經編輯你的ti TLE。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 – 2013-03-13 18:22:33

回答

0

我發現我的問題,我想填充第二個下拉時未使用正確的「選定值」