将dropdownlist绑定到数据库中的表 [英] binding dropdownlist to table in database

查看:94
本文介绍了将dropdownlist绑定到数据库中的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中有3个下拉列表和3个表。当我点击包含国家/地区的dropdownlist1(例如我选择美国)时我想要做什么然后自动处于状态ddl状态对应于美国发生在数据库中,同样点击状态来自城市。



我能用sqldatasource吗?怎么样?如果不是我可以这样做而不使用sql数据源?怎么办?

解决方案

你好b $ b

你有3个表格例如:



国家{id,name,region}

状态{id,country_id,name}查看country_id是外键国家表

city {id,state_id,name,time}看看state_id是状态表的外键



现在我们想要绑定Country表到ddl_country当然我假设你填写CountryDataTable:

 ddl_country.DataTextField =   name 
ddl_country.DataValueField = id
ddl_country.DataSource = CountryDataTable;
ddl_country.DataBind();



之后你应该绑定ddl_city,你应该去ddl_country SelectedIndexChanged 事件并编写此代码:

 ddl_city.DataTextField =   name 
ddl_city.DataValueField = id
strsql = select * from city where country_id ='& ddl_country.SelectedValue&安培; ';
// 之后你应该填写你的CityDataTable
ddl_city.DataSource = CityDataTable ;
ddl_city.DataBind();





你可以为其他ddl和实验室实验室做这项工作......; )

请点击此链接获取样品:

如何根据第一个组合的选择值填充第二个组合框[ ^ ]

这是非常好的样本:

http://www.dotnetfunda.com/forums/thread9657-binding-one-dropdownlist-to-another-dropdownlist.aspx [ ^ ]



最好的问候。

假设您有下拉列表ddl1(国家),ddl2(州),ddl3(城市)。您在ddl1中选择了国家/地区,然后为ddl1设置 AutoPostBack =true并在 SelectedIndexChanged 事件ddl1上绑定ddl2。现在你有在ddl1中选择的国家的州。



为ddl2做同样的事情(autopostback = true,在 SelectedIndexChanged ,绑定ddl3)。



问候..:)


如果你在谈论级联下拉列表,那么检查这些< br $> b $ b

选择动态级联下拉列表中的值 [ ^ ]

如何用另一个ddl填充下拉列表 [ ^ ]

Cascading DropDown - Country to state [ ^ ]



ASP.NET级联下拉列表(CSASPNETCascadingDropDownList) [ ^ ]

CascadingDropDown演示 [ ^ ]

ASP.NET MVC中的DropDownList [ ^ ]

在ASP.Net中创建级联DropDownLists [ ^ ]

将CascadingDropDown与数据库一起使用 [ ^ ]


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?

解决方案

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:

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:

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.


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.. :)


If you are talking about cascading dropdownlist then check these

Selecting value in cascading dropdown list dynamically[^]
How to populate a dropdown list with another ddl[^]
Cascading DropDown - Country to state[^]

ASP.NET cascading drop-down-list (CSASPNETCascadingDropDownList)[^]
CascadingDropDown Demonstration[^]
Cascading DropDownList in ASP.NET MVC[^]
Creating Cascading DropDownLists in ASP.Net[^]
Using CascadingDropDown with a Database[^]


这篇关于将dropdownlist绑定到数据库中的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆