-4

I want to populate TWO dropdownlist, based on selection of first dropdownlist the second dropdownlist will get populated.

Example :

Like i have two dropdownlist namely 1.ddlCountry and 2.ddlState

Now when country is selected then depending on the selected Country the States related with that Country will get populated in the State dropdownlist. I want to achieve this withour reloading the whole page in Asp.Net with coding language as C#.

How can i achieve the same?

Dropdownlist is fetching data from database by executing query.

Sam M
  • 1,077
  • 2
  • 20
  • 42
AyliyaJan
  • 41
  • 1
  • 7
  • Can you show us [what have you tried](http://whathaveyoutried.com) so far? – Blachshma Jan 02 '13 at 09:58
  • 1
    This is duplicate question and you also haven't searched for this problem. Find answer with this link of stackoverflow itself : http://stackoverflow.com/questions/12258839/populate-dropdown-list-based-on-another-dropdown-list – Dev Jan 02 '13 at 09:58
  • can you share your code what you have tried so far? – Mohammad Arshad Alam Jan 02 '13 at 09:59

3 Answers3

0
  1. You can use AJAX toolkit CascadingDropDown as told by Naresh. OR
  2. use ajax update panel and keep all Dropdowns in it. So the whole page will not load on changing dropdown value.

You didnt give the code to further solution.

syed mohsin
  • 2,948
  • 2
  • 23
  • 47
0
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
  FillStateByCountry();
}
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
  FillLocationByCountryandState();
}

private void FillStateByCountry()
{
    DataSet dstFillState;
    int CountryId = Convert.ToInt32(ddlCountry.SelectedValue.ToString());
    dstFillState = Tbl_State.FillDDLState(CountryId);
    ddlState.DataSource = dstFillState;
    ddlState.DataTextField = "State";
    ddlState.DataValueField = "Id";
    ddlState.DataBind();
}

similarly FillLocationByCountryandState();

sajanyamaha
  • 3,119
  • 2
  • 26
  • 44
0

Add two dropdownlists to your form and name it as cmbStates, cmbCities

when you select state name from cmbStates(dropwdownlist), cmbCities(dropdownlist) generates cities based on state name(cmbStates)

by fetching data from database

  private void Form1_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("server=pbs-server;database=p2p;user id=shekar;password=sekhar@1346");
        SqlCommand cmd = new SqlCommand("select states from Country", con);
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        con.Open();
        da.Fill(ds, "Country");
        cmbStates.DataSource = ds.Tables[0];
        cmbStates.SelectedValue = 0;
        con.Close();

    }

    private void cmbStates_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("server=xxxx;database=xxxx;user id=xxxxr;password=xxxxxx");
        SqlCommand cmd = new SqlCommand("select cities from States where cityname = 'cmbStates.SelectedItem.ToString()'", con);
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        con.Open();
        da.Fill(ds, "States");
        cmbCities.DataSource = ds.Tables[0];
        cmbCities.SelectedValue = 0;
        con.Close();           

    }

OR

manually adding items

namespace DropDownlist
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cmbStates.Items.Add("Andhra Pradesh");
            cmbStates.Items.Add("Tamilnadu");
            cmbStates.Items.Add("Karnataka");
            cmbStates.SelectedValue = 0;
        }

        private void cmbStates_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (cmbStates.SelectedItem.ToString() == "Andhra Pradesh")
            {
                cmbCities.Items.Clear();
                cmbCities.Items.Add("Hyderabad");
                cmbCities.Items.Add("Guntur");
                cmbCities.Items.Add("Vijayawada");
                cmbCities.SelectedValue = 0;

            }
            else if (cmbStates.SelectedItem.ToString() == "Tamilnadu")
            {
                cmbCities.Items.Clear();
                cmbCities.Items.Add("Chennai");
                cmbCities.Items.Add("Coimbatore");
                cmbCities.Items.Add("ooty");
                cmbCities.SelectedValue = 0;

            }
            else if (cmbStates.SelectedItem.ToString() == "Karnataka")
            {
                cmbCities.Items.Clear();
                cmbCities.Items.Add("Bangalore");
                cmbCities.Items.Add("Mangalore");
                cmbCities.SelectedValue = 0;

            }
            else
            {
                MessageBox.Show("Please Select any value");
            }
        }
    }
}