using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CountryStateDropDown
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        CountryCityEntities Db = new CountryCityEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            BindCountryDropdown();
        }
        void BindCountryDropdown()
        {
            DrCountry.DataSource = Db.Countries.ToList();
            DrCountry.Items.Clear();
            DrCountry.DataTextField = "Name";
            DrCountry.DataValueField = "Id";
            DrCountry.DataBind();

        }
        void BindSateDropDown()
        {
            int id = Convert.ToInt32(DrCountry.SelectedValue);
            DrState.DataSource = Db.States.Where(S => S.CountryId == id).ToList();
            DrState.Items.Clear();
            DrState.DataTextField = "Name";
            DrState.DataValueField = "Id";
            DrState.DataBind();
        }
        void BindCityDropDown()
        {
            int id = Convert.ToInt32(DrState.SelectedValue);
            DrCity.DataSource = Db.Cities.Where(C => C.StateId == id).ToList();
            DrCity.Items.Clear();
            DrCity.DataTextField = "Name";
            DrCity.DataValueField = "Id";
            DrCity.DataBind();
        }

        protected void DrCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindSateDropDown();
        }

        protected void DrState_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindCityDropDown();
        }
    }
}