Thursday, 2 January 2014

Cascading (Country,City)drop down list in asp.net


Developing a registration form with Country and city name .
I need the city to be loaded in the dropdown list based on the selection in country dropdownlist.


CODE:


   public void Bind_ddlCountry()
   {
       SqlConObject.Open();
       SqlCommand cmd = new SqlCommand("sp_Country", SqlConObject);
       cmd.CommandType = CommandType.StoredProcedure;

       SqlDataReader dr = cmd.ExecuteReader();
       ddlCountry.DataSource = dr;
       ddlCountry.Items.Clear();
       ddlCountry.Items.Add("Select");
       ddlCountry.DataTextField = "Country_Name";
       ddlCountry.DataValueField = "Country_ID";
       ddlCountry.DataBind();
       SqlConObject.Close();
   }


   public void Bind_ddlCity(int countryid)
   {
       SqlConObject.Open();
       SqlCommand cmd = new SqlCommand("sp_City", SqlConObject);
       cmd.CommandType = CommandType.StoredProcedure;
       cmd.Parameters.Add("@Countryid", SqlDbType.Int).Value = countryid;
       SqlDataReader dr = cmd.ExecuteReader();
       ddlCity.DataSource = dr;
       ddlCity.Items.Clear();
       ddlCity.Items.Add("Select");
       ddlCity.DataTextField = "City_Name";
       ddlCity.DataValueField = "Country_ID";
       ddlCity.DataBind();
       SqlConObject.Close();
      
   }
}
  protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind_ddlCity(int.Parse(ddlCountry.SelectedValue));
    }

 

No comments:

Post a Comment