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();
}
}