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

After every postback dropdownlist items repeats

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

例如 OnPage Load 我在下拉列表 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

更新

我有一个包含城市下拉列表的母版页。我正在对我的内容页面使用母版页控制,如下所示。

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

现在在我的 PopulateArea() class 中有一个查询,其中 WHERE 子句有 cityselector。所以根据城市获取我的区域。

它非常简单。每次 post 返回时都会触发页面加载事件。

使用这个

    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

在非回发事件中调用您的函数,这样就不会在回发事件中调用下拉列表(这会填充所有内容)。

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

如果您的下拉值必须在回发时更改。最初说 1,2,3,4 和回发时的值,如果值必须是 2,3,4,5 基于您在表单控件上更改的某些值,您将必须先清除下拉值,然后然后为其添加新值。

areasList.Items.Clear()

另请参阅 AppendDataBoundItems 不正确:在您的 .aspx 页面中

将您的代码更改为,

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