选择新项目后刷新 DropdownList 数据

发布于 2024-11-06 20:12:05 字数 887 浏览 0 评论 0原文

我有两个下拉列表,一个是州,一个是城市。选择州/省/自治区/直辖市后,城市下拉列表将被填充。此时,您可以选择一个城市,但是如果您选择一个城市并返回并选择不同的州,则城市下拉列表将由两个州的城市填充。如何明确第一个州的城市?

这是后面的代码

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Public Sub ddlState_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        SqlDataSource2.SelectParameters.Clear()
        SqlDataSource2.SelectParameters.Add(New Parameter("@State", DbType.String, ddlState.SelectedValue))
        ddlCity.DataBind()
        'SqlDataSource2.SelectParameters.Clear()
        'Dim State As String = ddlState.SelectedItem.Text
    End Sub

    Protected Sub ddlCity_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlCity.SelectedIndexChanged
        'Dim City As String = ddlCity.SelectedItem.Text
    End Sub

I have two dropdownlist, one is State and one is City. When a State is selected, the City dropdownlist becomes populated. At that point you can select a city, however if you select a city and go back and select a different state, the city dropdownlist is populated by the cities for both states. How do I get it clear the cities for the first state?

Here is the code behind

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Public Sub ddlState_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        SqlDataSource2.SelectParameters.Clear()
        SqlDataSource2.SelectParameters.Add(New Parameter("@State", DbType.String, ddlState.SelectedValue))
        ddlCity.DataBind()
        'SqlDataSource2.SelectParameters.Clear()
        'Dim State As String = ddlState.SelectedItem.Text
    End Sub

    Protected Sub ddlCity_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlCity.SelectedIndexChanged
        'Dim City As String = ddlCity.SelectedItem.Text
    End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转角预定愛 2024-11-13 20:12:05
 Public Sub ddlState_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)         
   SqlDataSource2.SelectParameters.Clear()         
   SqlDataSource2.SelectParameters.Add(New Parameter("@State", DbType.String, ddlState.SelectedValue))  
   ddlCity.Items.Clear();
   ddlCity.Items.Add(new ListItem("SELECT ONE", ""))  ' If you want it '       
   ddlCity.DataBind()
End Sub 

您还可以在控件中设置AppendDataboundItems = 'false'

 Public Sub ddlState_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)         
   SqlDataSource2.SelectParameters.Clear()         
   SqlDataSource2.SelectParameters.Add(New Parameter("@State", DbType.String, ddlState.SelectedValue))  
   ddlCity.Items.Clear();
   ddlCity.Items.Add(new ListItem("SELECT ONE", ""))  ' If you want it '       
   ddlCity.DataBind()
End Sub 

You could also set AppendDataboundItems = 'false' in the control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文