locked
droplist keeps going back to position one RRS feed

  • Question

  • User1298215938 posted

      If Not IsPostBack Then
                Dim query As String = "select Plant_ID, PlantName from PlantData"
                BindDropDownList(ddlCountries, query, "PlantName", "Plant_ID", "Select Plant")
                ddlStates.Enabled = False
                ddlCities.Enabled = False
                ddlStates.Items.Insert(0, New ListItem("Select Area", "0"))
                ddlCities.Items.Insert(0, New ListItem("Select Cell", "0"))
            End If

    Private Sub BindDropDownList(ddl As DropDownList, query As String, text As String, value As String, defaultText As String)
            Dim conString As String = ConfigurationManager.ConnectionStrings("ConString").ConnectionString
            Dim cmd As New SqlCommand(query)
            Using con As New SqlConnection(conString)
                Using sda As New SqlDataAdapter()
                    cmd.Connection = con
                    con.Open()
                    ddl.DataSource = cmd.ExecuteReader()
                    ddl.DataTextField = text
                    ddl.DataValueField = value
                    ddl.DataBind()
                    con.Close()
                End Using
            End Using
            ddl.Items.Insert(0, New ListItem(defaultText, "0"))
        End Sub

     Protected Sub Country_Changed(sender As Object, e As EventArgs)
            ddlStates.Enabled = False
            ddlCities.Enabled = False
            ddlStates.Items.Clear()
            ddlCities.Items.Clear()
            ddlStates.Items.Insert(0, New ListItem("Select Area", "0"))
            ddlCities.Items.Insert(0, New ListItem("Select Cell", "0"))
            Dim countryId As Integer = Integer.Parse(ddlCountries.SelectedItem.Value)
            If countryId > 0 Then
                Dim query As String = String.Format("select Area_ID, AreaName from Area_Data where Plant_ID = {0}", countryId)
                BindDropDownList(ddlStates, query, "AreaName", "Area_ID", "AreaName")
                ddlStates.Enabled = True


            End If
        End Sub

       Protected Sub State_Changed(sender As Object, e As EventArgs) Handles ddlStates.SelectedIndexChanged
            ddlCities.Enabled = False
            ddlCities.Items.Clear()
            ddlCities.Items.Insert(0, New ListItem("Select Cell", "0"))
            Dim stateId As Integer = Integer.Parse(ddlStates.SelectedItem.Value)
            If stateId > 0 Then
                Dim query As String = String.Format("select Area_ID, CellName from CellData where Area_ID = {0}", stateId)
                BindDropDownList(ddlCities, query, "CellName", "Area_ID", "Select Cell")
                ddlCities.Enabled = True
            End If
        End Sub

    the code works perfect I’m having a issue with the dllcities values will go back to the position one how can i disable this just for  dllcities?

    Friday, September 11, 2020 6:03 PM

Answers

  • User-939850651 posted

    Hi MOHIIMRAN,

    According to your description, I tried to test the code you provided, but the settings of properties in the drop-down controls on the page may affect the final result, but I do not know the content of the controls in the page, so I'm afraid that I can not reproduce your problem.

    And you said that AutoPostBack is disabled, which drop-down list is?

    From the code you provided, it seems that you are implementing the linkage function of the drop-down list, and you use the BindDropDownList function to fill these drop-down lists, then it will indeed display the first option value by default. So I don't know how your problem happened.

    What do you mean by enter that value into sql, is that add new options to the database?

    Please describe your problem more clearly, and provide a complete sample code, which will help us better find the problem and solve it.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 14, 2020 7:10 AM

All replies

  • User1298215938 posted

    I disabled the Autopostback too but when I try to enter that value into sql its goes back to position one again 

    Friday, September 11, 2020 7:18 PM
  • User-939850651 posted

    Hi MOHIIMRAN,

    According to your description, I tried to test the code you provided, but the settings of properties in the drop-down controls on the page may affect the final result, but I do not know the content of the controls in the page, so I'm afraid that I can not reproduce your problem.

    And you said that AutoPostBack is disabled, which drop-down list is?

    From the code you provided, it seems that you are implementing the linkage function of the drop-down list, and you use the BindDropDownList function to fill these drop-down lists, then it will indeed display the first option value by default. So I don't know how your problem happened.

    What do you mean by enter that value into sql, is that add new options to the database?

    Please describe your problem more clearly, and provide a complete sample code, which will help us better find the problem and solve it.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 14, 2020 7:10 AM
  • User-939850651 posted

    Hi MOHiMRAN,

    Any update?

    Friday, September 25, 2020 8:43 AM