This article shows how to work with a cascading dropdown list for country/state/city in ASP.Net using Entity Framework.
Create three tables as needed, as in:
CREATE TABLE country
(
countryID INT NOT NULL,
countryName varchar(50) NOT NULL,
PRIMARY KEY (countryID ),
);
CREATE TABLE state
(
stateID INT NOT NULL,
countryID INT NOTNULL,
stateName varchar(50) NOT NULL,
PRIMARY KEY (stateID ),
FOREIGN KEY (countryID ) REFERENCES country (countryID));
CREATE TABLE city
(
cityID INT NOT NULL,
stateID INT NOTNULL,
cityName varchar(50) NOT NULL,
PRIMARY KEY (cityID),
FOREIGN KEY (stateID) REFERENCES state (stateID));
First create an empty web application, as in:
![Image1.jpg]()
Add a model as we did earlier and do the necessary
![Image2.jpg]()
![Image3.jpg]()
Add a web page to the solution and copy and paste this into the .aspx page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="cascading.aspx.cs" Inherits="counrtybasedropdown.cascading" %>
<!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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<center>
<h3>
Cascading DropDownList for Country/State/City in ASP.Net</h3>
<table>
<tr>
<td>
Select a Country :
</td>
<td>
<asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
Select a State :
</td>
<td>
<asp:DropDownList ID="ddlState" runat="server" Enabled="false" AutoPostBack="true"
OnSelectedIndexChanged="ddlState_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
Select a City :
</td>
<td>
<asp:DropDownList ID="ddlCity" runat="server" Enabled="false">
</asp:DropDownList>
</td>
</tr>
</table>
</center>
</div>
</form>
</body>
</html>
Your .aspx.cs should be as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace counrtybasedropdown
{
public partial class cascading : System.Web.UI.Page
{
Database1Entities dbEntity = new Database1Entities();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var country = from c in dbEntity.countries select new { c.countryID, c.countryName };
ddlCountry.DataSource = country.ToList();
ddlCountry.DataValueField = "countryID";
ddlCountry.DataTextField = "countryName";
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, "--Select--");
}
}
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
int countyID = Convert.ToInt16(ddlCountry.SelectedValue.ToString());
var state = from s in dbEntity.states where s.countryID.Equals(countyID) select new { s.stateID, s.stateName };
ddlState.DataSource = state.ToList();
ddlState.Enabled = true;
ddlState.DataValueField = "stateID";
ddlState.DataTextField = "stateName";
ddlState.DataBind();
ddlState.Items.Insert(0, "--Select--");
}
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
int stateID = Convert.ToInt16(ddlState.SelectedValue.ToString());
var city = from c in dbEntity.cities where c.stateID.Equals(stateID) select new { c.cityID, c.cityName };
ddlCity.DataSource = city.ToList();
ddlCity.Enabled = true;
ddlCity.DataValueField = "cityID";
ddlCity.DataTextField = "cityName";
ddlCity.DataBind();
ddlCity.Items.Insert(0, "--Select--");
}
}
}
Happy coding.