locked
Pre-Populate DropDownList RRS feed

  • Question

  • User-381585723 posted

    I have a couple CascadingDropDown Lists, which are working properly. What I can't figure out is how to pre-populate the dropdownlists until a parent selection is made.

    Here's an example:

    Let's say I have two CascadingDropDowns: 1) Country 2) State

    When a user selects a country, the state dropdownlist is enabled and populated using the CascadingDropDown webservice. In this scenario, a user must select a Country to select State. Until a Country is selected, the state cascadingdropdown is disabled (I tried the EnableAtLoading but it failed to load any values in State). I want to popoulate the State DropDown with every state in the database until a Country is selected. At that time, the State list is set to those states associated with the selected country. I hope this makes sense.

    So, what I want to do is: Populate every state until a country is selected. At that time, the state ddl is reset to only those associated with that select country in the parent allowing the CascadingDropDown extender to override the dropdownlist data binding. The dropdownlist overrides the cascadingdropdown extender until a parent selection is made.

    How is this done?

    Here's my ASPX Markup (Keep in mind this works, but does not provide the needed functionality outlined above)

    <asp:DropDownList ID="ddlCountries" runat="server" Width="150">
            </asp:DropDownList>
            <ajaxToolkit:CascadingDropDown ID="cdlCountries" TargetControlID="ddlCountries" PromptText="Select Country"
               PromptValue="" ServicePath="LocationCascadeService.asmx" ServiceMethod="GetCountries" runat="server"
               Category="Country" LoadingText="Loading..." />
            <br /><br />
            <asp:DropDownList ID="ddlStates" runat="server" Width="150">
            </asp:DropDownList>
            <ajaxToolkit:CascadingDropDown ID="cdlStates" TargetControlID="ddlStates" PromptText="Select State"
                PromptValue="" ServicePath="LocationCascadeService.asmx" ServiceMethod="GetStates" runat="server"
                Category="State" ParentControlID="ddlCountries" LoadingText="Loading..." />
            <br /><br />
            <asp:DropDownList ID="ddlCounties" runat="server" Width="150">
            </asp:DropDownList>
            <ajaxToolkit:CascadingDropDown ID="cdlCounties" TargetControlID="ddlCounties" PromptText="Select County"
                PromptValue="" ServicePath="LocationCascadeService.asmx" ServiceMethod="GetCounties" runat="server"
                Category="County" ParentControlID="ddlStates" LoadingText="Loading..."  />

    Webservice Markup

    public class LocationCascadeHerpService : System.Web.Services.WebService
        {
            [WebMethod]
            public CascadingDropDownNameValue[] GetCountries(string knownCategoryValues)
            {
                string query = "SELECT DISTINCT(Country) FROM Location WHERE Country IS NOT NULL ORDER BY Country";
                List<CascadingDropDownNameValue> countries = GetData(query);
                return countries.ToArray();
            }
            [WebMethod]
            public CascadingDropDownNameValue[] GetStates(string knownCategoryValues)
            {
                string country = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)["Country"];
                string query = string.Format("SELECT DISTINCT State FROM Location WHERE Country = '{0}' AND State IS NOT NULL ORDER BY State", country);
                List<CascadingDropDownNameValue> states = GetData(query);
                return states.ToArray();
            }

            [WebMethod]
            public CascadingDropDownNameValue[] GetCounties(string knownCategoryValues)
            {
                string state = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)["State"];
                string query = string.Format("SELECT DISTINCT County FROM Location WHERE State = '{0}' AND County IS NOT NULL ORDER BY County", state);
                List<CascadingDropDownNameValue> counties = GetData(query);
                return counties.ToArray();
            }

            private List<CascadingDropDownNameValue> GetData(string query)
            {
                string conString = ConfigurationManager.ConnectionStrings["ResearchCollectionsConnectionString"].ConnectionString;
                SqlCommand command = new SqlCommand(query);
                List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
                using (SqlConnection con = new SqlConnection(conString))
                {
                    con.Open();
                    command.Connection = con;
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            values.Add(new CascadingDropDownNameValue
                            {
                                name = reader[0].ToString(),
                                value = reader[0].ToString()

                            });
                        }
                        reader.Close();
                        con.Close();
                        return values;
                    }
                }
            }

    Friday, December 27, 2013 2:12 PM

Answers

  • User-381585723 posted

    I rewrote the entire codebase, converting the ajaxtoolkit cascadingdropdowns to jQuery with success. In fact, I now have complete control over 5 cascading dropdownlists. I strongly suggest that anyone that runs into the problems associated with the limitations of the ajaxtoolkit, go with jQuery instead. 

    Following the above scenario, I set the initial value of each dropdown to a SQlDataSource, which was surprising difficult to accomplish using the ajaxtoolkit and essentially solvesd my initial problem. Parent selections are cascaded using jQuery change events.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 2, 2014 4:37 PM

All replies

  • User-933407369 posted

    hi mbnorton,

    Had you try to add the event for your controls, for example ddlCountries_SelectedIndexChanged, ddlStates_SelectedIndexChanged ...

    and then you can use the code below:

         ddlStates.Enabled = false; //disenabled ddlStates
         ddlCounties.Enabled = false;//disenabled ddlCounties
    

    Hope it helps you.

    Monday, December 30, 2013 4:08 AM
  • User-381585723 posted

    1. The OnChange events won't do the trick because the Drop-down or CCDs need to be populated on Page Load, before a parent selection is made.

    2. Enabled/Disabled won't do the trick either. I have tried the EnableAtLolading event of the CCD, then set the data source of the Drop-down in the Page Load event and a simple Sql Data Source assignment to no avail. The Drop-Down List/CCD was enabled on page load, but was not populated.

    Monday, December 30, 2013 11:40 AM
  • User-227760790 posted

    hi mbnorton,

    Can you post the complete  markup or provide a simple demo,which can easily reproduce the problem?

    I think that your Webservice Markup is correct, the issue should be ASPX Markup issue, so you need to provide the ASPX code-behind.

    Tks for your understanding.

    Monday, December 30, 2013 8:14 PM
  • User-381585723 posted

    I rewrote the entire codebase, converting the ajaxtoolkit cascadingdropdowns to jQuery with success. In fact, I now have complete control over 5 cascading dropdownlists. I strongly suggest that anyone that runs into the problems associated with the limitations of the ajaxtoolkit, go with jQuery instead. 

    Following the above scenario, I set the initial value of each dropdown to a SQlDataSource, which was surprising difficult to accomplish using the ajaxtoolkit and essentially solvesd my initial problem. Parent selections are cascaded using jQuery change events.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 2, 2014 4:37 PM