<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>
============================================================================
public partial class Demo2 : System.Web.UI.Page
{
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()
{
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
}
}