Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
if we select country in dropdownlist(like india) and then next dropdownlist show state ( like delhi) show in dropdownlist in asp.net
Posted

Try this:

First make AutoPostBack true Property in Drop_country Dropdown then

please write in page load :

C#
       if(!ispostback)
       {
              FillDropDownList();
       }
    

------>

 private void FillDropDownList()
    {
        DataSet ds = new DataSet();
        SqlDataAdapter myda = new SqlDataAdapter("select Country from table name",connection object);
        myda.Fill(ds);
        drop_country.DataSource = ds;
        drop_country.DataValueField = "Country";
        drop_country.DataBind();
    }

and double click on Drop_Country Dropdown
C#
protected void drop_country_SelectedIndexChanged(object sender, EventArgs e)
{
    DataSet ds = new DataSet();
        SqlDataAdapter myda = new SqlDataAdapter("select state from table name where country='"+Drop_contry.Selecteditem.Value+"'",connection object);
        myda.Fill(ds);
        drop_state.DataSource = ds;
        drop_state.DataValueField = "state";
        drop_state.DataBind();
}

if any Doubt please post it....
 
Share this answer
 
v2
Comments
M@anish 22-Aug-12 8:26am    
your code is really work but when i select country name like usa that time only one value show in another dropdownlist it mean only one value show from state so what should i do now
and thank for that.
AshishChaudha 22-Aug-12 8:36am    
What exactly you are doing and whats your requirement, please make it clear??
[no name] 22-Aug-12 8:48am    
it means the USA has Only one state come From Database...
AshishChaudha 22-Aug-12 8:35am    
my 5+
[no name] 22-Aug-12 8:47am    
Thanks Brotherrr
Hi ,
check this Solution
how to bind the dropdown list to another dropdown list[^]
Best Regards
M.Mitwalli
 
Share this answer
 
How to do this is widely documented on the web. I believe the ASP.NET AJAX library has controls for this if you prefer to not program, but doing it with AJAX itself via jquery is both trivial and widely documented.

You can also do a postback, but that would be ugly, in this day and age.
 
Share this answer
 
First make AutoPostBack true Property in Drop_country Drop down list after slow your error.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900