locked
VB.net droplist RRS feed

  • Question

  • User1298215938 posted

    Hi, I'm new to asp.net how do remove items from another droplist if valve equals to 33105?

    Dim Plant(2) As String
    Dim strplant As String
    Plant(0) = "N/A"
    Plant(1) = "33105"

    Array.Reverse(Plant)

    For Each strplant In Plant
    plantlist.Items.Add(strplant)
    Next

    Dim area(2) As String
    Dim strarea As String
    area(0) = "666"
    area(1) = "777"

    Array.Reverse(area)

    For Each strarea In area
    Arealist.Items.Add(strarea)
    Next

    If plantlist.SelectedIndex = 1 Then
    Arealist.Visible = "True"
    End If

    Monday, August 10, 2020 11:30 PM

Answers

  • User-1330468790 posted

    Hi MOHIIMRAN,

     

    Simply make a modification on the method "plantlist_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)" as below:

    Protected Sub plantlist_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
            If plantlist.SelectedValue = "33105" Then
                Arealist.Items.Remove("666")
            Else
                Dim area As String() = New String(1) {}
                area(0) = "666"
                area(1) = "777"
                Array.Reverse(area)
                Arealist.Items.Clear()
                For Each strarea As String In area
                    Arealist.Items.Add(strarea)
                Next
            End If
        End Sub

    Demo:

     

    However, if you have your own logic to bind the drop down list, you might need to consider change the code to fit your scenario.

     

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2020 8:42 AM

All replies

  • User-1330468790 posted

    Hi MOHIIMRAN,

     

    It looks like you want to use the event "OnSelectedIndexChanged" of the drop down list, which would be called when the selected item is changed. Bear in mind that you could turn on the "autopostback" flag so that the post will be triggered automatically when the selected item changes.

     

    You could refer to below codes to see if it explains how to implement the function you desired.

    ASPX

     <div>
                <asp:DropDownList ID="plantlist" runat="server" OnSelectedIndexChanged="plantlist_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
                <asp:DropDownList ID="Arealist" runat="server"></asp:DropDownList>
            </div>

    Code behind:

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                InitDDLs()
            End If
        End Sub
    
        Private Sub InitDDLs()
            Dim Plant As String() = New String(1) {}
            Plant(0) = "N/A"
            Plant(1) = "33105"
            // Here I removed the code line: Array.Reverse(Plant)
    //so that the default value of the drop down list will be "N/A"
    For Each strplant As String In Plant plantlist.Items.Add(strplant) Next Dim area As String() = New String(1) {} area(0) = "666" area(1) = "777" Array.Reverse(area) For Each strarea As String In area Arealist.Items.Add(strarea) Next If plantlist.SelectedIndex = 1 Then Arealist.Visible = True End If End Sub Protected Sub plantlist_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) If plantlist.SelectedValue = "33105" Then Arealist.Items.Clear() Else Dim area As String() = New String(1) {} area(0) = "666" area(1) = "777" Array.Reverse(area) For Each strarea As String In area Arealist.Items.Add(strarea) Next End If End Sub

    Demo:

     

    Best regards,

    Sean

    Tuesday, August 11, 2020 2:21 AM
  • User1298215938 posted

    thank you so much sean how can i write if plant = 3105 than to hide area 666?

    Tuesday, August 11, 2020 2:15 PM
  • User-1330468790 posted

    Hi MOHIIMRAN,

     

    Simply make a modification on the method "plantlist_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)" as below:

    Protected Sub plantlist_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
            If plantlist.SelectedValue = "33105" Then
                Arealist.Items.Remove("666")
            Else
                Dim area As String() = New String(1) {}
                area(0) = "666"
                area(1) = "777"
                Array.Reverse(area)
                Arealist.Items.Clear()
                For Each strarea As String In area
                    Arealist.Items.Add(strarea)
                Next
            End If
        End Sub

    Demo:

     

    However, if you have your own logic to bind the drop down list, you might need to consider change the code to fit your scenario.

     

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 12, 2020 8:42 AM
  • User1298215938 posted

    thanks sean

    Tuesday, August 18, 2020 3:24 AM