我正在尝试使用 2 个下拉菜单绑定(bind)数据。
我在 SQL Server 2008 中创建了 2 个表:
1)国家:
country_id (primary key)
country_name
2)状态:
state_id(primary key)
state_name
country_id(foreign_key)
现在我尝试将数据与 2 个下拉列表 ddp1
和 ddp2
绑定(bind),这样当我从第一个下拉列表中选择国家时 (ddp1
) 那么对应于该国家/地区的州列表应该出现在第二个下拉列表中 (ddp2
)。
有人可以帮我实现这个目标吗?
谢谢。
最佳答案
您可以使用更新面板来实现这一点。
1) 将两个控件放在一个更新面板中。 2) 将第一个下拉列表的 autopostback 属性设置为 true。 3) 使用 EventName="SelectedIndexChanged"向下拉列表的更新面板添加触发器 4)然后在后面的代码中有一个函数来处理第一个下拉列表的 SelectedIndexChanged 事件。在该函数中,绑定(bind)第二个下拉列表的数据。
大致会是这样(注:未测试)
<asp:UpdatePanel runat="server" ID="update1" UpdateMode="Conditional">
<asp:Dropdownlist runat="server" ID="firstddl" AutoPostBack="True"/>
<asp:Dropdownlist runat="server" ID="secondddl" />
<Triggers>
<asp:AsyncPostBackTrigger ControlID="firstddl" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
代码隐藏(再次未测试)
Protected Sub firstddl_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles firstddl.SelectedIndexChanged
'Update secondddl here
End Sub
关于c# - asp.net 和 c# 中的动态下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5583976/