Click here to Skip to main content
15,907,183 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am taking two dropdownlists, one for country and other one called states.
If I select country then I want to display all the states related to that country.

How can I write the code in asp.net?

Thanks in Advance
Posted
Updated 13-Sep-10 3:56am
v2
Comments
Dalek Dave 13-Sep-10 9:56am    
Edited for Grammar.

Hi,

Following is one way to do this,

1. Create a database table with all the states for the country.
2. Initially fetch the country name from the table.
3. On Dropdown change event, fetch the respective states from the table and display in dropdown 2.

Due to the database hit, the performance may affect.
You can also achieve this using javascript.

Regards,
Suresh
 
Share this answer
 
onSelectedIndexChanged of Country DropDown Write a Logic to Load Data to State Dropdown,
Store Country and State data Either to Database or ArrayList,
Like
ArrayList al = new ArrayList();
al.add('India');
al.add('London');

onSelectedIndexChanged of country Dropdown Put condition of DataCheck Like
C#
if(drpCountry.SelectedItem.Text == "India")
{
//Load States of India to Another DropDown.
}

Thanks,
 
Share this answer
 
Hi Ganesh!Exactly what Suresh is Correct.you just create a database.If don't have DB do one thing create two Generic Lists of type String .
Like this

public List<String> states = new List<String>();


If u select any item in dropdown1 take the index value using switch u can do it to show the items in dropdown2.

You can also use JavaScript
 
Share this answer
 
You can use ASP.NET AJAX Cascading Dropdown[^]

Go through the sample here: Using CascadingDropDown with a Database[^]

Hope this helps!
 
Share this answer
 
Hi,
steps
1.u should fill two dropdown lists ie,coutry and state
2.Then u write code in country's selected index changed,
note: must give autopostback true(country dropdownlist)

hope this helps.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 12-Aug-11 11:13am    
No text speak please! Read the FAQ!
Here is the best example for what you want. Please have a look.

http://javascript.about.com/library/bl3drop.htm[^]

Mohan Gajula
 
Share this answer
 
Comments
Ankur\m/ 14-Sep-10 2:53am    
Your example needs all the names of the state and country to be present at the client side which is a very bad design. What if a new state is added to a country. You will have to go to the code and add it again.

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