Cascading Dropdown List in ASP.Net Using Entity Framework

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:

  1. CREATE TABLE country  
  2.   (  
  3.      countryID     INT NOT NULL,  
  4.      countryName   varchar(50) NOT NULL,  
  5.      PRIMARY KEY (countryID ),  
  6.   );  
  7. CREATE TABLE state  
  8.   (  
  9.  stateID     INT NOT NULL,  
  10.  countryID INT NOTNULL,  
  11.  stateName   varchar(50) NOT NULL,  
  12.  PRIMARY KEY (stateID ),  
  13.  FOREIGN KEY (countryID ) REFERENCES country (countryID));  
  14.   
  15. CREATE TABLE city  
  16.   (  
  17. cityID     INT NOT NULL,  
  18.  stateID INT NOTNULL,  
  19.  cityName   varchar(50) NOT NULL,  
  20.  PRIMARY KEY (cityID),  
  21.  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:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="cascading.aspx.cs" Inherits="counrtybasedropdown.cascading" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head id="Head1" runat="server">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div>  
  10.         <center>  
  11.             <h3>  
  12.                 Cascading DropDownList for Country/State/City in ASP.Net</h3>  
  13.             <table>  
  14.                 <tr>  
  15.                     <td>  
  16.                         Select a Country :  
  17.                     </td>  
  18.                     <td>  
  19.                         <asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged">  
  20.                         </asp:DropDownList>  
  21.                     </td>  
  22.                 </tr>  
  23.                 <tr>  
  24.                     <td>  
  25.                         Select a State :  
  26.                     </td>  
  27.                     <td>  
  28.                         <asp:DropDownList ID="ddlState" runat="server" Enabled="false" AutoPostBack="true"                        OnSelectedIndexChanged="ddlState_SelectedIndexChanged">  
  29.                         </asp:DropDownList>  
  30.                     </td>  
  31.                 </tr>  
  32.                 <tr>  
  33.                     <td>  
  34.                         Select a City :  
  35.                     </td>  
  36.                     <td>  
  37.                         <asp:DropDownList ID="ddlCity" runat="server" Enabled="false">  
  38.                         </asp:DropDownList>  
  39.                     </td>  
  40.                 </tr>  
  41.             </table>  
  42.         </center>  
  43.     </div>  
  44.     </form>  
  45. </body>  
  46. </html>   

Your .aspx.cs should be as follows:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. namespace counrtybasedropdown  
  8. {  
  9.     public partial class cascading : System.Web.UI.Page  
  10.     {  
  11.         Database1Entities dbEntity = new Database1Entities();  
  12.         protected void Page_Load(object sender, EventArgs e)  
  13.         {  
  14.             if (!IsPostBack)  
  15.             {  
  16.                 var country = from c in dbEntity.countries select new { c.countryID, c.countryName };  
  17.                 ddlCountry.DataSource = country.ToList();  
  18.                 ddlCountry.DataValueField = "countryID";  
  19.                 ddlCountry.DataTextField = "countryName";  
  20.                 ddlCountry.DataBind();  
  21.                 ddlCountry.Items.Insert(0, "--Select--");  
  22.             }  
  23.         }  
  24.         protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)  
  25.         {  
  26.             int countyID = Convert.ToInt16(ddlCountry.SelectedValue.ToString());  
  27.             var state = from s in dbEntity.states where s.countryID.Equals(countyID) select new { s.stateID, s.stateName };  
  28.             ddlState.DataSource = state.ToList();  
  29.             ddlState.Enabled = true;  
  30.             ddlState.DataValueField = "stateID";  
  31.             ddlState.DataTextField = "stateName";  
  32.             ddlState.DataBind();  
  33.             ddlState.Items.Insert(0, "--Select--");  
  34.         }  
  35.         protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)  
  36.         {  
  37.             int stateID = Convert.ToInt16(ddlState.SelectedValue.ToString());  
  38.             var city = from c in dbEntity.cities where c.stateID.Equals(stateID) select new { c.cityID, c.cityName };  
  39.             ddlCity.DataSource = city.ToList();  
  40.             ddlCity.Enabled = true;  
  41.             ddlCity.DataValueField = "cityID";  
  42.             ddlCity.DataTextField = "cityName";  
  43.             ddlCity.DataBind();  
  44.             ddlCity.Items.Insert(0, "--Select--");  
  45.         }  
  46.     }  
  47. }   

Happy coding.


Similar Articles