Hi Team,
I have two dropdown List and second dropdown value will bind based on selecting first one . I am using vb net as language. So What approch I can follow as OnSelectedIndexChanged event we cannot use in vb. Please help if any.
Hi Team,
I have two dropdown List and second dropdown value will bind based on selecting first one . I am using vb net as language. So What approch I can follow as OnSelectedIndexChanged event we cannot use in vb. Please help if any.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Rajeesh MenothRank #118 · 18.4K reputationPosted Apr 6, 2023, 12:16 PM EDT
Hi,
Go through the following article for VB - https://www.aspsnippets.com/Articles/Populate-Cascading-DropDownList-from-Database-in-ASPNet-Example.aspx
Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 6, 2023, 2:44 PM EDT
function BindSecondDropdown(selectedValue) { $.ajax({ type: "POST", url: "PageName.aspx/PopulateSecondDropdown", data: '{selectedValue: "' + selectedValue + '" }', contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { var ddlSecond = document.getElementById(''); ddlSecond.innerHTML = ""; for (var i = 0; iProtected Sub ddlFirst_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlFirst.SelectedIndexChanged ' Clear the items of the second dropdown list ddlSecond.Items.Clear() ' Get the selected value of the first dropdown list Dim selectedValue As String = ddlFirst.SelectedValue ' Bind the items of the second dropdown list based on the selected value of the first dropdown list If selectedValue = "Option 1" Then ddlSecond.Items.Add(New ListItem("Option A", "1")) ddlSecond.Items.Add(New ListItem("Option B", "2")) ElseIf selectedValue = "Option 2" Then ddlSecond.Items.Add(New ListItem("Option C", "3")) ddlSecond.Items.Add(New ListItem("Option D", "4")) End If End SubAmit MohantyRank #112 · 20K reputationPosted Apr 6, 2023, 11:55 PM EDT
In VB.NET, you can use the
SelectedIndexChangedevent of the first dropdown list to dynamically populate the second dropdown list.First, create the two dropdown lists and the first dropdown list has the
AutoPostBackproperty set toTrueand theOnSelectedIndexChangedevent set toddlDropDownList_SelectedIndexChanged.Then, in your code-behind file, add the following code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then 'Populate the second dropdown list with the default values based on the first dropdown list' PopulateSecondDropDownList(ddlDropDownList.SelectedValue) End If End Sub Protected Sub ddlDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) 'Populate the second dropdown list based on the selected value of the first dropdown list' PopulateSecondDropDownList(ddlDropDownList.SelectedValue) End Sub Private Sub PopulateSecondDropDownList(ByVal selectedValue As String) 'Clear the items in the second dropdown list' ddlDropDownList2.Items.Clear() 'Populate the items in the second dropdown list based on the selected value of the first dropdown list' Select Case selectedValue Case "1" ddlDropDownList2.Items.Add(New ListItem("Option 1.1", "1.1")) ddlDropDownList2.Items.Add(New ListItem("Option 1.2", "1.2")) ddlDropDownList2.Items.Add(New ListItem("Option 1.3", "1.3")) Case "2" ddlDropDownList2.Items.Add(New ListItem("Option 2.1", "2.1")) ddlDropDownList2.Items.Add(New ListItem("Option 2.2", "2.2")) ddlDropDownList2.Items.Add(New ListItem("Option 2.3", "2.3")) Case "3" ddlDropDownList2.Items.Add(New ListItem("Option 3.1", "3.1")) ddlDropDownList2.Items.Add(New ListItem("Option 3.2", "3.2")) ddlDropDownList2.Items.Add(New ListItem("Option 3.3", "3.3")) End Select End Sub