每个回发下拉列表项重复后

时间:2016-03-13 06:37:37

标签: asp.net vb.net databound

我已将我的下拉列表与数据库绑定。但是在每个PostBack上,下拉列表中的项目会再次重复。

e.g。 OnPage加载我在下拉列表1,2,3,4等中有这些项目现在假设页面获得postBack然后它看起来像1,2,3,4,1,2,3,4

Vb代码

Private Sub hospitals_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.PopulateAreas()
End Sub


Private Sub PopulateAreas()
        If IsPostBack Then
            Dim citySelector As DropDownList = Page.Master.FindControl("locationSelector")
            Using conn As New MySqlConnection()
                conn.ConnectionString = ConfigurationManager _
                    .ConnectionStrings("conio").ConnectionString()
                Using cmd As New MySqlCommand()
                    cmd.CommandText = "Select * from areas where areaCity Like '" + citySelector.SelectedItem.ToString + "%'"
                    cmd.Connection = conn
                    conn.Open()
                    Using sdr As MySqlDataReader = cmd.ExecuteReader()
                        While sdr.Read()
                            Dim item As New ListItem()
                            item.Text = sdr("areaName").ToString()
                            item.Value = sdr("areaID").ToString()
                            'item.Selected = Convert.ToBoolean(sdr("IsSelected"))
                            areasList.Items.Add(item)
                        End While
                    End Using
                    conn.Close()
                End Using
            End Using
        End If

更新

我有拥有城市下拉列表的母版页。我正在使用母版页控制到mycontent页面,如下所示。

Dim citySelector As DropDownList = Page.Master.FindControl("locationSelector")

现在在我的PopulateArea()类中有一个查询,其中WHERE子句有cityselector。因此,根据城市我的区域得到了。

4 个答案:

答案 0 :(得分:3)

在内部调用你的函数而不是回发事件,这样就不会在回发事件上调用下拉列表(这会将所有东西填满)。

Private Sub hospitals_Load(sender As Object, e As EventArgs) Handles Me.Load
    if ispostback = false then
        Me.PopulateAreas()
    end if
End Sub

答案 1 :(得分:1)

非常简单..每次发回任何内容时都会触发页面加载事件。

使用此

    Private Sub hospitals_Load(sender As Object, e As EventArgs) Handles Me.Load
      If Page.IsPostBack Then

        //do stuff when post back occurs

      Else

        Me.PopulateAreas() //put this function here so that it executes only once
      End If
    End Sub

答案 2 :(得分:1)

如果您的下拉值必须在回发上更改。如果值必须为2,3,4,5,则根据您在表单控件上更改的某些值,首先说明1,2,3,4和回发时的值,您必须首先清除下拉值和然后为其添加新值。

areasList.Items.Clear()

另请注意AppendDataBoundItems不是真的:在您的.aspx页面

答案 3 :(得分:0)

将您的代码更改为

Private Sub PopulateAreas()
    If IsPostBack Then
        areasList.Items.clear()
        Dim citySelector As DropDownList = Page.Master.FindControl("locationSelector")
        Using conn As New MySqlConnection()
            conn.ConnectionString = ConfigurationManager _
                .ConnectionStrings("conio").ConnectionString()
            Using cmd As New MySqlCommand()
                cmd.CommandText = "Select * from areas where areaCity Like '" + citySelector.SelectedItem.ToString + "%'"
                cmd.Connection = conn
                conn.Open()
                Using sdr As MySqlDataReader = cmd.ExecuteReader()
                    While sdr.Read()
                        Dim item As New ListItem()
                        item.Text = sdr("areaName").ToString()
                        item.Value = sdr("areaID").ToString()
                        'item.Selected = Convert.ToBoolean(sdr("IsSelected"))
                        areasList.Items.Add(item)
                    End While
                End Using
                conn.Close()
            End Using
        End Using
    End If
End Sub