After every postback dropdownlist items repeats

2019-08-13 23:45发布

I have bind my dropdownlist with database. But on each PostBack the items in dropdownlist gets repeat again.

e.g. OnPage Load I have this items in dropdownlist 1,2,3,4.. etc Now suppose page gets postBack then it looks like 1,2,3,4,1,2,3,4

Vb code

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

UPDATE

I have master page which has dropdownlist of cities. I am using masterpage control to mycontent page like below.

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

Now in my PopulateArea() class there is query in which WHERE clause have cityselector. So according to city my area gets fetched.

4条回答
Emotional °昔
2楼-- · 2019-08-14 00:16

If your dropdown values have to change on the postback. Say initially the values where 1,2,3,4 and on postback if the values has to be 2,3,4,5 based on some of the values you change on the form controls you will have to clear the dropdown values first and then add new values to it.

areasList.Items.Clear()

Also see that AppendDataBoundItems is not true: in your .aspx page

查看更多
乱世女痞
3楼-- · 2019-08-14 00:22

its pretty simple.. the page load event is triggered every time anything is post back.

use this

    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
查看更多
贼婆χ
4楼-- · 2019-08-14 00:24

Change your code to,

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
查看更多
该账号已被封号
5楼-- · 2019-08-14 00:33

Call your function inside not postback event so that the dropdown is not called on postback events(which fills it everything).

Private Sub hospitals_Load(sender As Object, e As EventArgs) Handles Me.Load
    if ispostback = false then
        Me.PopulateAreas()
    end if
End Sub
查看更多
登录 后发表回答