Click here to Skip to main content
15,911,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 3 dropdownlist and 3 tables in database. what i want to do when i click dropdownlist1 that contains countries (eg i selct America) then automatically in states ddl states corresponding to America occurs from database and similarly on clicking states comes cities.

can i do it with sqldatasource?how? and if not than can i do this without using sql datasource?how?
Posted

Suppose you have dropdowns ddl1(country),ddl2(state),ddl3(City). you selected country in ddl1,then set AutoPostBack="true" for ddl1 and bind ddl2 on SelectedIndexChanged event of ddl1.Now you have states for the country selected in ddl1.

Do the same for ddl2(autopostback=true,in SelectedIndexChanged,bind ddl3).

Regards.. :)
 
Share this answer
 
Hi
You have 3 table for example:

Country{id,name,region}
State{id,country_id,name} look at country_id is a foreign key for the Country table
city{id,state_id,name,time} look at state_id is a foreign key for the State table

Now we want to the bind Country table to ddl_country of course I assume you are fill the CountryDataTable:
C#
ddl_country.DataTextField ="name"
ddl_country.DataValueField ="id"
ddl_country.DataSource= CountryDataTable;
ddl_country.DataBind();

After that you should bind the ddl_city for do that you should go to the ddl_country SelectedIndexChanged event and write this code:
C#
ddl_city.DataTextField ="name"
ddl_city.DataValueField ="id"
strsql = "select * from city where country_id=' " & ddl_country.SelectedValue& "'";
//after that you should fill your CityDataTable 
ddl_city.DataSource = CityDataTable;
ddl_city.DataBind();


and you can do this work for other ddl and lab lab lab ... ;)
Please follow of this link for get a sample:
How to fill the second combobox on depending on the selectedvalue of the first[^]
It is really good sample for this purpose:
http://www.dotnetfunda.com/forums/thread9657-binding-one-dropdownlist-to-another-dropdownlist.aspx[^]

Best Regards.
 
Share this answer
 
v3
Comments
Rambo_Raja 17-Jun-13 8:40am    
ddl_country.DisplayMember ="name"
ddl_country.ValueMember ="id"
ddl_country.DataSource = CountryDataTable;
ddl_country.DataBind();
where i have to write this code..on select index of ddlcountry displaymember property is not coming...
Aydin Homay 17-Jun-13 8:44am    
Check it again I have modified ;)
Rambo_Raja 17-Jun-13 9:10am    
thanx....i m near about to completion ...
Aydin Homay 17-Jun-13 23:17pm    
My pleasure sir ;)

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