1
votes

I have three drop down list ddl1, ddl2 and ddl3.

Now on selecting an item of ddl1 the respective items in ddl2 and similarly ddl3 should be populated.

Eg:- If I select "India" in ddl1 then ddl2 should display all states and ddl3 should display all cities of country India. I wish to do this without any database connectivity( just static from html). Please help!!!. Thanks.

5
So you are going to hard-code all of the data? Are you using UpdatePanels?Belogix
mannually means what ??Rajeev Kumar
use collections to do manually.watraplion
ARe you kidding ? May be you want to do it in ajax. On selecting india, All the states should immediately get loaded in the state..?Sakthivel

5 Answers

0
votes
  private SqlConnection conn = your connection string;
    public void Bind_ddlcountry()
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("select countryid,country from tblcountry", conn);
        SqlDataReader dr = cmd.ExecuteReader();
        ddlcountry.DataSource = dr;
        ddlcountry.Items.Clear();
        ddlcountry.Items.Add("--Please Select country--");
        ddlcountry.DataTextField = "Country";
        ddlcountry.DataValueField = "CountryId";
        ddlcountry.DataBind();
        conn.Close();
    }

// Simillarly bind your state and city ..

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)

        {    Bind_ddlcountry(); }

    }
    protected void ddlcountry_selectedindexchanged(object sender, EventArgs e)
    {
        Bind_ddlstate();
        ddlstate.Focus();
    }
    protected void ddlstate_selectedindexchanged(object sender, EventArgs e)
    {
        Bind_ddlcity();
        ddlcity.Focus();
    }
0
votes

If you are fetching country data from database, you should fetch all the state and city data together with it.

Then you should filter data with dataview. So only 1 database call will fulfill your needs.

It may help you.

0
votes

If you want to avoid hitting the database 3 times you can do like this,

  1. Retrieve the data to a Dataset ( Can write a stored procedure to return all the info like Country, state and City)

Dataset ds= //Get from SP

  1. Assign the country data to Datatable and bind to the country dropdownlist

                   DataTable country=ds.Tables[0];
                   DataTable state=ds.Tables[1];
                   DataTable city=ds.Tables[2];
                   if (countryTab != null)
                   {
                       ddlCountries.DataSource = countryTab;
                       ddlCountries.DataTextField = "country_name";
                       ddlCountries.DataValueField = "country_code";
                       ddlCountries.DataBind();
                   }
    
  2. On SelectedIndexChanged event of the country dropdown write query to filter the the state info and bind to the State dropdown list

     DataRow[] r = table[1].Select("County like '%" + <selected country value> + "%'");
    
  3. And bind the filtered result set to the State dropdown list. Same method is applicable for city dropdown also.

0
votes

I had a same situation some days ago , it should be accomplished something like populating all the data in lists on page load and then filtering the lists via jquery and ajax and dynamically binding them to dropdowns. I tried but didn't succeeded. so I used Ajaxtoolkit's cascading dropdowns.

0
votes

as you are not linking to a database you will be better of using an IfElse Statement. something along the lines of

if (dropdownlist1.selectedValue == "India")
{
    dropdownlist2.items.clear();
    dropdownlist2.items.add("CityName");
    dropdownlist2.items.add("CityName");
    dropdownlist2.items.add("CityName");
    dropdownlist2.items.add("CityName");
    dropdownlist2.items.add("CityName");
    dropdownlist2.items.add("CityName");
    dropdownlist2.items.add("CityName");
}

copy and past this for each country, it is very repetitive but effective.