Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

In My case using cascade dropdownlist for Country,State,City and MainRoad.
It works fine on selection but i want to that when country items change onkeyup or onkeydown that time load depended state items same for state,city or MainRoad. How can do this please provide help full solutions.

My code is
ASP.NET
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1"  runat="server">
    </cc1:ToolkitScriptManager>
   <div>
        <table cellpadding="3" cellspacing="3">
            <tr>
                <td>Country</td>
                <td>
                    <asp:DropDownList ID="dr_country" runat="server" >
                    </asp:DropDownList>
                   
                    <cc1:CascadingDropDown ID="cas_country"  runat="server"
                     Category="Country" 
                TargetControlID="dr_country" UseContextKey="true" LoadingText="Loading ......"
                PromptText="Select Country" PromptValue="" ServiceMethod="BindCountrydropdown" 
                ServicePath="CascadeDropDownService.asmx">
                    </cc1:CascadingDropDown>
                </td>
            </tr>
            <tr>
            <td>State</td>
                <td>
                <asp:DropDownList ID="dr_state" runat="server">
                    </asp:DropDownList>
                    <cc1:CascadingDropDown ID="cas_state"  runat="server"
                    Category="State" 
                TargetControlID="dr_State" ParentControlID="dr_country" LoadingText="Loading ......" 
                PromptText="Select State" PromptValue="" ServiceMethod="BindStatedropdown" 
                ServicePath="CascadeDropDownService.asmx">
                    </cc1:CascadingDropDown>
                </td>
            </tr>
            <tr>
            <td>City</td>
                <td>
                <asp:DropDownList ID="dr_city" runat="server" >
                    </asp:DropDownList>
                    <cc1:CascadingDropDown ID="cas_city"  runat="server"
                        Category="Region" TargetControlID="dr_city" ParentControlID="dr_State" PromptValue=""
                        LoadingText="Loading Cities..." PromptText="Select City" 
                        ServiceMethod="BindCitydropdown" ServicePath="CascadeDropDownService.asmx">
                    </cc1:CascadingDropDown>
                </td>
            </tr>
            <tr>
            <td>MainRoad</td>
                <td>
                <asp:DropDownList ID="dr_mainroad" runat="server">
                    </asp:DropDownList>
                    <cc1:CascadingDropDown ID="cas_mainroad"  runat="server"
                        Category="Mainroad" TargetControlID="dr_MainRoad" ParentControlID="dr_city" PromptValue=""
                        LoadingText="Loading Main Road..." PromptText="Select Mainroad"
                        ServiceMethod="BindMainroadropdown" ServicePath="CascadeDropDownService.asmx">
                    </cc1:CascadingDropDown>
                </td>
            </tr>
        </table>
   </div>



CascadeDropDownService.asmx

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using AjaxControlToolkit;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Collections.Specialized;

namespace PMSCloud.CascadeDropDown
{
    /// <summary>
    /// Summary description for CascadeDropDownService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.Web.Script.Services.ScriptService()]
    [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 CascadeDropDownService : System.Web.Services.WebService
    {
        #region CommonIdTitle for Country,State,City,MainRoad
        
        [WebMethod]
        public CascadingDropDownNameValue[] BindCountrydropdown(string knownCategoryValues, string category, string contextKey)
        {
            int SubscriberId = 0;
            if (contextKey != "")
            { SubscriberId = Convert.ToInt32(contextKey); }
            SqlConnection concountry = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
            concountry.Open();
            SqlCommand cmdcountry = new SqlCommand("SP_Get_Mst_Country_Id_Title", concountry);
            cmdcountry.CommandType = CommandType.StoredProcedure;
            cmdcountry.Parameters.AddWithValue("@SubscriberId", SubscriberId);
            SqlDataAdapter dacountry = new SqlDataAdapter(cmdcountry);
            cmdcountry.ExecuteNonQuery();
            DataSet dscountry = new DataSet();
            dacountry.Fill(dscountry);
            concountry.Close();
            List<CascadingDropDownNameValue> countrydetails = new List<CascadingDropDownNameValue>();
            foreach (DataRow dtrow in dscountry.Tables[0].Rows)
            {
                string CountryID = dtrow["Id"].ToString();
                string CountryName = dtrow["Title"].ToString();
                countrydetails.Add(new CascadingDropDownNameValue(CountryName, CountryID));
            }
            return countrydetails.ToArray();
        }
        [WebMethod]
        public CascadingDropDownNameValue[] BindStatedropdown(string knownCategoryValues, string category)
        {
            int CountryID;
            StringDictionary countrydetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
            CountryID = Convert.ToInt32(countrydetails["Country"]);
            SqlConnection constate = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
            constate.Open();
            SqlCommand cmdstate = new SqlCommand("SP_Get_Mst_State_Id_Title", constate);
            cmdstate.CommandType = CommandType.StoredProcedure;
            cmdstate.Parameters.AddWithValue("Country_Id", CountryID);
            cmdstate.ExecuteNonQuery();
            SqlDataAdapter dastate = new SqlDataAdapter(cmdstate);
            DataSet dsstate = new DataSet();
            dastate.Fill(dsstate);
            constate.Close();
            List<CascadingDropDownNameValue> statedetails = new List<CascadingDropDownNameValue>();
            foreach (DataRow dtstaterow in dsstate.Tables[0].Rows)
            {
                string stateID = dtstaterow["Id"].ToString();
                string statename = dtstaterow["Title"].ToString();
                statedetails.Add(new CascadingDropDownNameValue(statename, stateID));
            }
            return statedetails.ToArray();
        }
        [WebMethod]
        public CascadingDropDownNameValue[] BindCitydropdown(string knownCategoryValues, string category)
        {
            int stateID;
            StringDictionary statedetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
            stateID = Convert.ToInt32(statedetails["State"]);
            SqlConnection conregion = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
            conregion.Open();
            SqlCommand cmdregion = new SqlCommand("SP_Get_Mst_City_Id_Title", conregion);
            cmdregion.CommandType = CommandType.StoredProcedure;
            cmdregion.Parameters.AddWithValue("State_Id", stateID);
            cmdregion.ExecuteNonQuery();
            SqlDataAdapter daregion = new SqlDataAdapter(cmdregion);
            DataSet dsregion = new DataSet();
            daregion.Fill(dsregion);
            conregion.Close();
            List<CascadingDropDownNameValue> regiondetails = new List<CascadingDropDownNameValue>();
            foreach (DataRow dtregionrow in dsregion.Tables[0].Rows)
            {
                string regionID = dtregionrow["Id"].ToString();
                string regionname = dtregionrow["Title"].ToString();
                regiondetails.Add(new CascadingDropDownNameValue(regionname, regionID));

            }
            return regiondetails.ToArray();
        }
        [WebMethod]
        public CascadingDropDownNameValue[] BindMainroadropdown(string knownCategoryValues, string category)
        {
            int City;
            StringDictionary citydetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
            City = Convert.ToInt32(citydetails["Region"]);
            SqlConnection conmainroad = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
            conmainroad.Open();
            SqlCommand cmdmainroad = new SqlCommand("SP_Get_Mst_MainRoad_Id_Title", conmainroad);
            cmdmainroad.CommandType = CommandType.StoredProcedure;
            cmdmainroad.Parameters.AddWithValue("City_Id", City);
            cmdmainroad.ExecuteNonQuery();
            SqlDataAdapter dacity = new SqlDataAdapter(cmdmainroad);
            DataSet dsmainroad = new DataSet();
            dacity.Fill(dsmainroad);
            conmainroad.Close();
            List<CascadingDropDownNameValue> mainroaddetails = new List<CascadingDropDownNameValue>();
            foreach (DataRow dtmainroadrow in dsmainroad.Tables[0].Rows)
            {
                string mainroadID = dtmainroadrow["Id"].ToString();
                string mainraodname = dtmainroadrow["Title"].ToString();
                mainroaddetails.Add(new CascadingDropDownNameValue(mainraodname, mainroadID));

            }
            return mainroaddetails.ToArray();
        }
        #endregion
}
}
Posted

1 solution

No, onkeyup and onkeydown can't be integrated with CascadingDropDown. The way you want to work with them is not the usual way.

The child DropDownList should only be populated on selection of parent DropDownList, not on its keyup or key down. Otherwise it will flicker each time user presses up and down keys. That would also annoy the user and make your application heavy.

Please don't implement this. :)
 
Share this answer
 
Comments
rahul003 7-Jun-14 4:51am    
Thanks for quick replay. it browser supporting problems for CascadingDropDown. Mozila not support it but Chrome supported.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900