When selecting value from country drop-down list it is resetting the value of all other drop-down list boxes and the selected country is also getting reset.
The country drop-down list, state drop-down list and district drop-down list are dependent.
private void BindDropDownList(DropDownList ddl, string query, string text, string value, string defaultText)
{
string conString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
con.Open();
ddl.DataSource = cmd.ExecuteReader();
ddl.DataTextField = text;
ddl.DataValueField = value;
ddl.DataBind();
con.Close();
}
}
ddl.Items.Insert(0, new ListItem(defaultText, "0"));
}
protected void gCountry2_SelectedIndexChanged(object sender, EventArgs e)
{
gState2.Enabled = false;
gDistrict2.Enabled = false;
gState2.Items.Clear();
gDistrict2.Items.Clear();
//gState2.Items.Insert(0, new ListItem("Select State", "0"));
//gDistrict2.Items.Insert(0, new ListItem("Select City", "0"));
int countryId = int.Parse(gCountry2.SelectedItem.Value);
if (countryId > 0)
{
string query = string.Format("select StateId, StateName from States where CountryId = {0}", countryId);
BindDropDownList(gState2, query, "StateName", "StateId", "Select State");
gState2.Enabled = true;
Page.SetFocus(f2.ClientID);
}
}
protected void gState2_SelectedIndexChanged1(object sender, EventArgs e)
{
gDistrict2.Enabled = false;
gDistrict2.Items.Clear();
//gDistrict2.Items.Insert(0, new ListItem("Select City", "0"));
int stateId = int.Parse(gState2.SelectedItem.Value);
if (stateId > 0)
{
string query = string.Format("select CityId, CityName from Cities where StateId = {0}", stateId);
BindDropDownList(gDistrict2, query, "CityName", "CityId", "Select City");
gDistrict2.Enabled = true;
Page.SetFocus(f2.ClientID);
}