After every postback dropdownlist items repeats

半城伤御伤魂 提交于 2020-01-05 14:59:40

问题


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.


回答1:


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



回答2:


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



回答3:


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:


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



来源:https://stackoverflow.com/questions/35967331/after-every-postback-dropdownlist-items-repeats

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!