UI
<asp:DropDownList ID="ddlCountries" runat="server" AutoPostBack="true" AppendDataBoundItems="true"
OnSelectedIndexChanged="ddlCountries_SelectedIndexChanged" Width="100%" class="form-control">
</asp:DropDownList>
<asp:DropDownList ID="ddlStates" runat="server" AutoPostBack ="true" AppendDataBoundItems="true"
Width="100%" class="form-control">
</asp:DropDownList>
.cs Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindCountryDDL();
BindStateDDL(countryID);
}
}
private void BindCountryDDL()
{
List<CountriesEL> countryLst = new List<CountriesEL>();
countryLst = objCountryBL.GetAllActiveCountries();
if (countryLst != null && countryLst.Count > 0)
{
ddlCountries.DataTextField = "CountryName";
ddlCountries.DataValueField = "CountryId";
ddlCountries.DataSource = countryLst;
ddlCountries.DataBind();
}
ddlCountries.Items.Insert(0, new ListItem("--Select Country--", "0"));
ddlCountries.SelectedIndex = 0;
}
private void BindStateDDL(long CountryID)
{
List<StatesEL> stateLst = new List<StatesEL>();
ddlStates.Items.Clear();
int countryID = Convert.ToInt32(ddlCountries.SelectedValue);
dt = new DataTable();
if (objCountryEL.CountryId > 0)
{
dt = objStateBL.GetStateByCountryID(countryID);
if (dt.Rows.Count > 0)
{
ddlStates.DataSource = dt;
ddlStates.DataTextField = "StateName";
ddlStates.DataValueField = "StateId";
ddlStates.DataBind();
}
}
protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e)
{
objCountryEL.CountryId = Convert.ToInt32(ddlCountries.SelectedValue);
BindStateDDL(countryID);
ddlStates.Attributes.Add("StateName", ddlStates.SelectedValue);
ddlStates.Attributes.Add("stateID", ddlStates.SelectedValue);
ddlStates.Attributes.Add("CountryId", ddlStates.SelectedValue);
}
<asp:DropDownList ID="ddlCountries" runat="server" AutoPostBack="true" AppendDataBoundItems="true"
OnSelectedIndexChanged="ddlCountries_SelectedIndexChanged" Width="100%" class="form-control">
</asp:DropDownList>
<asp:DropDownList ID="ddlStates" runat="server" AutoPostBack ="true" AppendDataBoundItems="true"
Width="100%" class="form-control">
</asp:DropDownList>
.cs Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindCountryDDL();
BindStateDDL(countryID);
}
}
private void BindCountryDDL()
{
List<CountriesEL> countryLst = new List<CountriesEL>();
countryLst = objCountryBL.GetAllActiveCountries();
if (countryLst != null && countryLst.Count > 0)
{
ddlCountries.DataTextField = "CountryName";
ddlCountries.DataValueField = "CountryId";
ddlCountries.DataSource = countryLst;
ddlCountries.DataBind();
}
ddlCountries.Items.Insert(0, new ListItem("--Select Country--", "0"));
ddlCountries.SelectedIndex = 0;
}
private void BindStateDDL(long CountryID)
{
List<StatesEL> stateLst = new List<StatesEL>();
ddlStates.Items.Clear();
int countryID = Convert.ToInt32(ddlCountries.SelectedValue);
dt = new DataTable();
if (objCountryEL.CountryId > 0)
{
dt = objStateBL.GetStateByCountryID(countryID);
if (dt.Rows.Count > 0)
{
ddlStates.DataSource = dt;
ddlStates.DataTextField = "StateName";
ddlStates.DataValueField = "StateId";
ddlStates.DataBind();
}
}
protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e)
{
objCountryEL.CountryId = Convert.ToInt32(ddlCountries.SelectedValue);
BindStateDDL(countryID);
ddlStates.Attributes.Add("StateName", ddlStates.SelectedValue);
ddlStates.Attributes.Add("stateID", ddlStates.SelectedValue);
ddlStates.Attributes.Add("CountryId", ddlStates.SelectedValue);
}