我想要创建的是:我想在asp.net中创建2个下拉列表。让我们说,第一个包含国家,第二个包含这些国家的城市。
假设我在第一个dropdownlist列表中选择了英格兰,那么在第二个下拉列表中,只有属于英格兰的城市是可见的或可选择的。
<asp:DropDownList ID="Countries" runat="server"> // First dropdownlist
<asp:ListItem>England</asp:ListItem>
<asp:ListItem>Denmark</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="Citys" runat="server"> // Second dropdownlist
</asp:DropDownList>我希望我解释得足够好,我希望有人能为我提供一些代码来做这件事,或者给我一些提示。
发布于 2013-11-15 19:55:15
如果使用SqlDataSource,则可以将选定的值级联到CitySqlDataSource。
这里的演示-
数据库
Countries Cities
Id Name Id CountryId Name
1 England 1 1 London
2 Denmark 2 2 Copenhagen ASPX
<asp:DropDownList ID="CountryDropDownList" runat="server"
DataSourceID="CountrySqlDataSource"
DataTextField="Name" DataValueField="Id" AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="CountrySqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT Id,Name FROM [Countries]"></asp:SqlDataSource>
<asp:DropDownList ID="CityDropDownList" runat="server"
DataSourceID="CitySqlDataSource"
DataTextField="Name" DataValueField="Id">
</asp:DropDownList>
<asp:SqlDataSource ID="CitySqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT Id,Name FROM Cities WHERE CountryId=@CountryId">
<SelectParameters>
<asp:ControlParameter ControlID="CountryDropDownList"
PropertyName="SelectedValue"
Name="CountryId " Type="String"
DefaultValue="England" />
</SelectParameters>
</asp:SqlDataSource>下面是类似的回答。
发布于 2013-11-15 19:29:56
它被称为级联下拉列表,我建议查看ASP.NET AJAX CascadingDropDownList控件。
阅读在数据库中使用CascadingDropDown获得教程。
本质上,您将创建多个常规的ASP.NET下拉列表服务器控件(在您的情况下是两个),以及一个级联下拉列表服务器控件,用于您想要的每一级级联。这就是把两个下拉列表结合在一起的原因。
所有的数据获取都是通过ASP.NET AJAX方法完成的,这些方法本质上是页面托管的web服务方法,没有对页面本身的引用,因为它们是静态的,并且很适合通过脚本(即客户端)向服务器询问数据并返回它。
注意:默认情况下,方法将数据编码为JSON,因此除非您希望返回JSON以外的内容(即ASP.NET ),否则不会看到任何编码逻辑。
发布于 2013-11-15 19:53:34
https://stackoverflow.com/questions/20008761
复制相似问题