Populate 2nd dropdown by first dropdown value in asp.net vb

后端 未结 3 1435
温柔的废话
温柔的废话 2021-01-25 13:22

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 wo

相关标签:
3条回答
  • 2021-01-25 13:58

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

    ASPX

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

    VB

    Private Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddl1.SelectedIndexChanged
        If ddl1.SelectedValue = "1" Then
            ddl2.SelectedValue = "C"
        End If
    End Sub
    
    0 讨论(0)
  • 2021-01-25 14:09

    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"
    }
    
    0 讨论(0)
  • 2021-01-25 14:10

    You can use Javascript Onchange event.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript">
            function NumbersDropDownList_OnChange() {
                var numbersDropDownList = document.getElementById("numbersDropDownList");
                if (numbersDropDownList.options[numbersDropDownList.selectedIndex].text=="1") {
                    document.getElementById("lettersDropDownList").selectedIndex = 2;
                }
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="numbersDropDownList" onchange="NumbersDropDownList_OnChange()" runat="server">
                <asp:ListItem>1</asp:ListItem>
                <asp:ListItem>2</asp:ListItem>
                <asp:ListItem>3</asp:ListItem>
            </asp:DropDownList>
            <asp:DropDownList ID="lettersDropDownList" runat="server">
                <asp:ListItem>a</asp:ListItem>
                <asp:ListItem>b</asp:ListItem>
                <asp:ListItem>c</asp:ListItem>
            </asp:DropDownList>
        </div>
        </form>
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题