Quantcast
Channel: ASP.NET AJAX + Ajax Control Toolkit (ACT)
Viewing all articles
Browse latest Browse all 5678

Pre-Populate DropDownList

$
0
0

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


Viewing all articles
Browse latest Browse all 5678

Trending Articles