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.
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
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
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
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
Member
26 Points
49 Posts
VB.net droplist
Aug 10, 2020 11:30 PM|MOHIIMRAN|LINK
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
Contributor
3020 Points
889 Posts
Re: VB.net droplist
Aug 11, 2020 02:21 AM|Sean Fang|LINK
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:
Demo:
Best regards,
Sean
Member
26 Points
49 Posts
Re: VB.net droplist
Aug 11, 2020 02:15 PM|MOHIIMRAN|LINK
thank you so much sean how can i write if plant = 3105 than to hide area 666?
Contributor
3020 Points
889 Posts
Re: VB.net droplist
Aug 12, 2020 08:42 AM|Sean Fang|LINK
Hi MOHIIMRAN,
Simply make a modification on the method "plantlist_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)" as below:
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
Member
26 Points
49 Posts
Re: VB.net droplist
Aug 18, 2020 03:24 AM|MOHIIMRAN|LINK
thanks sean