Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
CSS
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script src="Scripts/jquery-1.8.2.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#<%=ddlState.ClientID %>').attr('disabled', 'disabled');
    $('#<%=ddlRegion.ClientID %>').attr('disabled', 'disabled');
    $('#<%=ddlState.ClientID %>').append('<option selected="selected" value="0">Select State</option>');
    $('#<%=ddlRegion.ClientID %>').empty().append('<option selected="selected" value="0">Select Region</option>');
    $('#<%=ddlCountry.ClientID %>').change(function () {
        var country = $('#<%=ddlCountry.ClientID%>').val()
    $('#<%=ddlState.ClientID %>').removeAttr("disabled");
    $('#<%=ddlRegion.ClientID %>').empty().append('<option selected="selected" value="0">Select Region</option>');
    $('#<%=ddlRegion.ClientID %>').attr('disabled', 'disabled');
    $.ajax({
        type: "POST",
        url: "Demo2.aspx/BindStates",
        data: "{'country':'" + country + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            var j = jQuery.parseJSON(msg.d);
            var options;
            for (var i = 0; i < j.length; i++) {
                options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>'
            }
            $('#<%=ddlState.ClientID %>').html(options)
},
    error: function (data) {
        alert('Something Went Wrong')
    }
});
});
    $('#<%=ddlState.ClientID %>').change(function () {
        var stateid = $('#<%=ddlState.ClientID%>').val()
    $('#<%=ddlRegion.ClientID %>').removeAttr("disabled");
    $.ajax({
        type: "POST",
        url: "Demo2.aspx/BindRegion",
        data: "{'state':'" + stateid + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            var j = jQuery.parseJSON(msg.d);
            var options;
            for (var i = 0; i < j.length; i++) {
                options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>'
            }
            $('#<%=ddlRegion.ClientID %>').html(options)
},
    error: function (data) {
        alert('Something Went Wrong')
    }
});
})
})
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Maincontent" runat="server">
    <center>
        <table style="width:100%">
            <tr>
                <td colspan="2" class="tdHeader1">
                    Cascade
                </td>
            </tr>
            <tr>
                <td class="label">
                    State :
                </td>
                <td class="labelDescription">
                    <asp:DropDownList ID="ddlCountry" runat="server"></asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td class="label">
                    District :
                </td>
                <td class="labelDescription">
                    
                    <asp:DropDownList ID="ddlState" runat="server"></asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td class="label">
                    City :
                </td>
                <td class="labelDescription">
                   <asp:DropDownList ID="ddlRegion" runat="server"></asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td class="label"></td>
                <td class="labelDescription"></td>
            </tr>
        </table>
    </center>
</asp:Content>


============================================================================

C#
 public partial class Demo2 : System.Web.UI.Page
    {

        //public static string strcon = "Data Source=OTMUMHODTN004/SA;Initial Catalog=INDIACAST;User ID=sa;Password=saadmin@123;Integrated Security=true";
        public static string strcon = ConfigurationManager.ConnectionStrings["Indiacastcms"].ConnectionString;

        #region Page_Load
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCountries();
            }
        }
        #endregion

        
       #region BindCountries()
        public void BindCountries()
        {
            //String strQuery = "select CountryID,CountryName from MasterCountry";
            //using (SqlConnection con = new SqlConnection(strcon))
            //{
            //    using (SqlCommand cmd = new SqlCommand())
            //    {
            //        cmd.CommandType = CommandType.Text;
            //        cmd.CommandText = strQuery;
            //        cmd.Connection = con;
            //        con.Open();
            //        ddlCountry.DataSource = cmd.ExecuteReader();
            //        ddlCountry.DataTextField = "CountryName";
            //        ddlCountry.DataValueField = "CountryID";
            //        ddlCountry.DataBind();
            //        ddlCountry.Items.Insert(0, new ListItem("<<--Select Country-->>", "0"));
            //        con.Close();
            //    }
            //}
            try
            {
                strSel = "select CountryID,CountryName from MasterCountry";
                ds = common.ExecuteDataSet(strSel,CommandType.Text);

                if(ds.Tables[0].Rows.Count > 0)
                {
                    ddlCountry.DataSource=ds.Tables[0];
                    ddlCountry.DataValueField = "CountryID";
                    ddlCountry.DataTextField = "CountryName";
                    ddlCountry.DataBind();
                    ddlCountry.Items.Insert(0,"<<--Select Country-->>");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region [WebMethod] BindStates(string country)
        [WebMethod]
        public static string BindStates(string country)
        {
            StringWriter builder = new StringWriter();
            String strQuery = "select StateID,StateName from MasterState where CountryID=@CountryID";
            DataSet ds = new DataSet();
            using (SqlConnection con = new SqlConnection(strcon))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = strQuery;
                    cmd.Parameters.AddWithValue("@countryid", country);
                    cmd.Connection = con;
                    con.Open();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(ds);
                    con.Close();
                }
            }
            DataTable dt = ds.Tables[0];
            builder.WriteLine("[");
            if (dt.Rows.Count > 0)
            {
                builder.WriteLine("{\"optionDisplay\":\"<<--Select State-->>\",");
                builder.WriteLine("\"optionValue\":\"0\"},");
                for (int i = 0; i <= dt.Rows.Count - 1; i++)
                {
                    builder.WriteLine("{\"optionDisplay\":\"" + dt.Rows[i]["StateName"] + "\",");
                    builder.WriteLine("\"optionValue\":\"" + dt.Rows[i]["StateID"] + "\"},");
                }
            }
            else
            {
                builder.WriteLine("{\"optionDisplay\":\"--Select State--\",");
                builder.WriteLine("\"optionValue\":\"0\"},");
            }
            string returnjson = builder.ToString().Substring(0, builder.ToString().Length - 3);
            returnjson = returnjson + "]";
            return returnjson.Replace("\r", "").Replace("\n", "");
        }
        #endregion

        #region [WebMethod] BindRegion(string state)
        [WebMethod]
        public static string BindRegion(string state)
        {
            StringWriter builder = new StringWriter();
            String strQuery = "select RegionID, RegionName from MasterRegion where StateID=@StateID";
            DataSet ds = new DataSet();
            using (SqlConnection con = new SqlConnection(strcon))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = strQuery;
                    cmd.Parameters.AddWithValue("@StateID", state);
                    cmd.Connection = con;
                    con.Open();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(ds);
                    con.Close();
                }
            }
            DataTable dt = ds.Tables[0];
            builder.WriteLine("[");
            if (dt.Rows.Count > 0)
            {
                builder.WriteLine("{\"optionDisplay\":\"<<--Select Region-->>\",");
                builder.WriteLine("\"optionValue\":\"0\"},");
                for (int i = 0; i <= dt.Rows.Count - 1; i++)
                {
                    builder.WriteLine("{\"optionDisplay\":\"" + dt.Rows[i]["RegionName"] + "\",");
                    builder.WriteLine("\"optionValue\":\"" + dt.Rows[i]["RegionID"] + "\"},");
                }
            }
            else
            {
                builder.WriteLine("{\"optionDisplay\":\"--Select Region--\",");
                builder.WriteLine("\"optionValue\":\"0\"},");
            }
            string returnjson = builder.ToString().Substring(0, builder.ToString().Length - 3);
            returnjson = returnjson + "]";
            return returnjson.Replace("\r", "").Replace("\n", "");
        }
        #endregion
    }
}
Posted
Updated 30-Nov-14 22:59pm
v3
Comments
Thanks7872 1-Dec-14 2:26am    
Just dumping a code here won't help you or make it a question. Remove unnecessary code block. Point out problamatic code and ask specific question only.

Use Improve question link at bottom of the question.

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