Baffled by CascadingDropDown

Last post 05-21-2009 8:13 AM by Zhi-Qiang Ni - MSFT. 2 replies.

Sort Posts:

  • Baffled by CascadingDropDown

    05-18-2009, 7:13 AM
    • Member
      164 point Member
    • mark4asp
    • Member since 03-14-2007, 10:13 AM
    • Posts 223

    Here is my example.

    When a value in selConsultant is changed selOffice should be filled.

    In my test examples the value of knownCategoryValues is

    undefined:0;
      or
    undefined:6;

    The number 6 is a ConsultantID which will return a populated List<ConsultantOffice>
    The number 0 is a ConsultantID which will return a an empty List<ConsultantOffice>

    In both cases when I run the GetOffices() method categoryValues["ConsultantID"] is found to be null.

    The display of selOffice always shows: "Please select an office", although it does refresh when the consultant is changed.

    Q1: Why does it not work?

    Q2: Do I need to set the AutoPostBack property of selConsultant to true or false?

    If I need to set it to true then why is that not indicated in the example code?

    No matter, setting it to true or false changes nothing.

    Q3: What about EnableViewState must this be set to true for all the DropDownLists?

    Appendix 1: Properties of the Consultant and ConsultantOffice objects.

    public class Consultant
    {
      public int ConsultantID { get; set; }
      public string ConsultantName { get; set; }
    }
    
    public class ConsultantOffice
    {
      public int ConsultantOfficeID { get; set; }
      public int ConsultantID { get; set; }
      public string OfficeName { get; set; }
    }

    Appendix 2: The essential elements on the page:

     <asp:Content id="Content1" contentplaceholderid="cph1" runat="Server">
      <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server"
    EnableViewState="true"> <Services> <asp:ServiceReference Path="/Service/Offices.asmx" /> </Services> </asp:ScriptManagerProxy> <asp:DropDownList ID="selConsultant" runat="server"
    DataValueField="ConsultantID" DataTextField="ConsultantName" /> <cc1:CascadingDropDown id="casOffices" runat="server" ParentControlID="selConsultant" TargetControlID="selOffice" Category="ConsultantOffice" PromptText="Please select an office" PromptValue="0" LoadingText="[Loading Offices...]" ServicePath="/Service/Offices.asmx" ServiceMethod="GetOffices" SelectedValue="0" /> <span>Optionally by office:</span> <asp:DropDownList id="selOffice" runat="server"
    DataValueField="ConsultantOfficeID" DataTextField="OfficeName" /> </asp:Content>
     

    Appendix 3: Relevant code:

        // This is the web method to load the CascadingDropDown
        [WebMethod]
        public CascadingDropDownNameValue[] GetOffices(string knownCategoryValues, string category)
        {
          StringDictionary categoryValues = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
          int consultantID = Convert.ToInt32(categoryValues["ConsultantID"]);
          List<CascadingDropDownNameValue> cascadingValues = new List();
          List offices = ConsultantOffices.GetOffices(consultantID);
          foreach (ConsultantOffice o in offices)
          {
            cascadingValues.Add(new CascadingDropDownNameValue(o.OfficeName, o.ConsultantOfficeID.ToString()));
          }
          return cascadingValues.ToArray();
        }
    
    
        // This is how the DropDownLists are loaded in code-behind
        protected void Page_Init(object sender, EventArgs e)
        {
          if (!Page.IsPostBack)
          {
            selConsultant.DataSource = Consultants.GetConsultants_For_DDL();
            selConsultant.DataBind();
            selConsultant.Items.Insert(0, new ListItem("All", "0"));
    
            selOffice.DataSource = ConsultantOffices.GetOffices(SearchState.ConsultantID);
            selOffice.DataBind();
          }
        }
    
     
  • Re: Baffled by CascadingDropDown

    05-18-2009, 3:46 PM
    • Member
      164 point Member
    • mark4asp
    • Member since 03-14-2007, 10:13 AM
    • Posts 223

    I gave up on this and wrote one using jQuery instead. Despite me knowing nothing about jQuery I was able to get it working in less time than I wasted with the AjaxControlToolkit CascadingDropDown.

    I posted my code in the hints and tips forum.

  • Re: Baffled by CascadingDropDown

    05-21-2009, 8:13 AM
    Answer

    Hi mark4asp,

    Q1: Why does it not work?

    The cause is the first DropDownList is not associated with a CascadingDropDown, then the child control can not get the parent’s category form the selection of DropDownList. Thus, the knownCategoryValues should always be undefined.

    The related thread:
    http://forums.asp.net/p/1251458/2317590.aspx#2317590
    http://forums.asp.net/p/1263859/2380702.aspx#2380702

    Q2: Do I need to set the AutoPostBack property of selConsultant to true or false?

    The CascadingDropDown’s server method would be fired whether the Parent’s AutoPostBack value is set as true or false. If the AutoPostBack is false,  the CascadingDropDown’s onParentChange function would be fired and then the ServerMethod can get the corresponding value from the accessed knownCategoryValues. If the AutoPostBack is true, the selection of the parent control would be saved, and the ServerMethod can still get the correct value when its client initialize function is fired.

    Q3: What about EnableViewState must this be set to true for all the DropDownLists?

    Maybe you have already thought of the reason, yes, it is to save the selection of the DropDownList

    I recommend you have a look at my sample threads about the usage of CascadingDropDown:
    http://forums.asp.net/p/1251458/2317590.aspx#2317590
    http://forums.asp.net/p/1256084/2344897.aspx#2344897
    http://forums.asp.net/p/1260534/2365488.aspx#2365488
    http://forums.asp.net/p/1263859/2380702.aspx#2380702
    http://forums.asp.net/p/1274181/2429707.aspx#2429707
    http://forums.asp.net/p/1319523/2626490.aspx#2626490

    You can also refer to these video tutorials and download the sample code from them:
    http://www.asp.net/learn/ajax-videos/video-77.aspx
    http://www.asp.net/learn/ajax-videos/video-278.aspx

    Best regards,

    Zhi-Qiang Ni

     

    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as
    Answer” if a marked post does not actually answer your question.
Page 1 of 1 (3 items)