Skip to content Skip to sidebar Skip to footer

Populate 2nd Dropdown By First Dropdown Value In Asp.net Vb

I'm having a bit of trouble using asp.net vb What I want to do is have 2 dropdown boxes The first dropdown would have 1 2 3 for example. the second dropdown would have a b c by de

Solution 1:

You can do it server side or in Java Script. The general concept is the same though. You have to target the second dropdown in the "change" event of the first dropdown. Meaning whenever the change event fires for the first one, you update the second one

Sudo code:

Dropdown1_Changed()
{
  //if "1" is selected in Dropdown1, update Dropdown2 to select"c"
}

Solution 2:

I would proboby use the SelectedIndexChanged event on the first dropdownlist. Like this:

ASPX

<asp:DropDownListID="ddl1"runat="server"AutoPostBack="true"><asp:ListItemText="1"Value="1"></asp:ListItem><asp:ListItemText="2"Value="2"></asp:ListItem><asp:ListItemText="3"Value="3"></asp:ListItem></asp:DropDownList><asp:DropDownListID="ddl2"runat="server"><asp:ListItemText="A"Value="A"></asp:ListItem><asp:ListItemText="B"Value="B"></asp:ListItem><asp:ListItemText="C"Value="C"></asp:ListItem></asp:DropDownList>

VB

PrivateSub ddl1_SelectedIndexChanged(ByVal sender AsObject, ByVal e As System.EventArgs) Handles ddl1.SelectedIndexChanged
    If ddl1.SelectedValue = "1"Then
        ddl2.SelectedValue = "C"EndIfEndSub

Solution 3:

You can use Javascript Onchange event.

<htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title></title><scripttype="text/javascript">functionNumbersDropDownList_OnChange() {
            var numbersDropDownList = document.getElementById("numbersDropDownList");
            if (numbersDropDownList.options[numbersDropDownList.selectedIndex].text=="1") {
                document.getElementById("lettersDropDownList").selectedIndex = 2;
            }
        }
    </script></head><body><formid="form1"runat="server"><div><asp:DropDownListID="numbersDropDownList"onchange="NumbersDropDownList_OnChange()"runat="server"><asp:ListItem>1</asp:ListItem><asp:ListItem>2</asp:ListItem><asp:ListItem>3</asp:ListItem></asp:DropDownList><asp:DropDownListID="lettersDropDownList"runat="server"><asp:ListItem>a</asp:ListItem><asp:ListItem>b</asp:ListItem><asp:ListItem>c</asp:ListItem></asp:DropDownList></div></form></body></html>

Post a Comment for "Populate 2nd Dropdown By First Dropdown Value In Asp.net Vb"