aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebForm2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="js/jquery_1.8.3.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
function fillsubcountry() {
var countryval = document.getElementById("<%=ddlcountry.ClientID %>").value;
var response = WebForm2.state(countryval);
binddrops(response.value, "<%=ddlstate.ClientID %>");
}
function fillsubstate() {
var stateval = document.getElementById("<%=ddlstate.ClientID %>").value;
var response = WebForm2.city(stateval);
binddrops(response.value, "<%=ddlcity.ClientID %>");
}
function binddrops(dictObj, ddlObj) {
$('#' + ddlObj).empty();
$('#' + ddlObj).append($("<option>").attr('value', '-1').text('--Select--'));
$(dictObj).each(function () {
$('#' + ddlObj).append($("<option>").attr('value', this.Key).text(this.Value));
})
}
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlcountry" runat="server" onchange="return fillsubcountry();">
</asp:DropDownList>
<asp:DropDownList ID="ddlstate" runat="server" onchange="return fillsubstate();">
</asp:DropDownList>
<asp:DropDownList ID="ddlcity" runat="server">
</asp:DropDownList>
</div>
</form>
</body>
</html>
aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(WebForm2), Page);
if (!IsPostBack)
{
country();
}
}
public void country()
{
List<Country> c = new List<Country>();
c.Add(new Country
{
id = "1",
name = "india"
});
c.Add(new Country
{
id = "2",
name = "usa"
});
ddlcountry.DataValueField = "id";
ddlcountry.DataTextField = "name";
ddlcountry.DataSource = c;
ddlcountry.DataBind();
ddlcountry.Items.Insert(0, new ListItem("--Select--", "0"));
}
[Ajax.AjaxMethod]
public Dictionary<string, string> state(string val)
{
Dictionary<string, string> value = new Dictionary<string, string>();
List<State> s = new List<State>();
s.Add(new State
{
id = "1",
fid = "1",
name = "maharashtra"
});
s.Add(new State
{
id = "2",
fid = "1",
name = "MP"
});
s.Add(new State
{
id = "3",
fid = "1",
name = "AP"
});
s.Add(new State
{
id = "4",
fid = "2",
name = "texas"
});
s.Add(new State
{
id = "5",
fid = "2",
name = "glasgo"
});
var f = from c in s
where c.fid.Equals(val)
select c;
value = f.ToDictionary(x => x.id, x => x.name);
return value;
}
[Ajax.AjaxMethod]
public Dictionary<string, string> city(string val)
{
Dictionary<string, string> value = new Dictionary<string, string>();
List<City> t = new List<City>();
t.Add(new City
{
id = "1",
fid = "1",
name = "mumbai"
});
t.Add(new City
{
id = "2",
fid = "1",
name = "goa"
});
t.Add(new City
{
id = "3",
fid = "2",
name = "mp_asas"
});
t.Add(new City
{
id = "4",
fid = "2",
name = "mp_weasd"
});
t.Add(new City
{
id = "5",
fid = "3",
name = "ap_asas"
});
t.Add(new City
{
id = "6",
fid = "4",
name = "texas_weasd"
});
t.Add(new City
{
id = "7",
fid = "5",
name = "glasgo_weasd"
});
var f = from c in t
where c.fid.Equals(val)
select c;
value = f.ToDictionary(x => x.id, x => x.name);
return value;
}
}
class Country
{
public string id { get; set; }
public string name { get; set; }
}
class State
{
public string id { get; set; }
public string fid { get; set; }
public string name { get; set; }
}
class City
{
public string id { get; set; }
public string fid { get; set; }
public string name { get; set; }
}
web.config
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax"/>
</httpHandlers>
<compilation debug="true" targetFramework="4.0">
</compilation>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web>
</configuration>