选择新项目后刷新DropdownList数据

时间:2011-05-10 19:52:22

标签: asp.net vb.net drop-down-menu

我有两个下拉列表,一个是State,一个是City。选择“状态”后,将填充“城市”下拉列表。此时,您可以选择一个城市,但是如果您选择一个城市并返回并选择其他州,则城市下拉列表将由两个州的城市填充。如何明确第一个州的城市?

以下是

背后的代码
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

1 个答案:

答案 0 :(得分:2)

 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'