locked
Dropdown event that loads another dropdownlist on load RRS feed

  • Question

  • User2015884762 posted

    Hi

    What's the event that I can use to populate another dropdownlist once the parent dropdown list is loaded with a certain value. Perhaps I should explain it better:

    I have a cascaded asp dropdownlist. Instead of using onSelectedIndexChanged, the value of the parent dropdown is selected automatically set by javascript function. Hence, I need the child drop down to show the corresponding values without the user changing any values at the parent dropdown.

    How can I do that. 

    Tuesday, April 2, 2019 8:51 AM

All replies

  • User475983607 posted

    The question is a little confusing.  It sounds like you know values and selections  ahead of time.   Why can't you render the values from the server rather than complicating matter with client side code?

    Tuesday, April 2, 2019 11:46 AM
  • User-1174608757 posted

    Hi callykalpana,

    According to your description,if you  want to set the options of dropdownlist in javascript, you first should add an option to it ,then you could set the val() of dropdownlist as what you want,then if you want realize the connection of two dropdownlist, I suggest you that you could put the method in parent dropdownlist change function in javascript. Here is the demo, I hope it could help you.

    <head runat="server">
        <title></title>
        <script src="../Scripts/jquery-3.3.1.js"></script>
        <script>
            $(function ()
            {
                //set default value of dropdownlist
              $("#<%=DropDownList2.ClientID%>").append("<option>JACK's Age</option>")
                $("#<%=DropDownList2.ClientID%>").val("JACK's Age")
                
                //build logic in parent change function
                $("#<%=DropDownList1.ClientID%>").change(function () {
                    if ($(this).val().trim() == "TOM") {
                        //empty the dropdownlist first
                        $("#<%=DropDownList2.ClientID%>").empty()
                        //append option
                        $("#<%=DropDownList2.ClientID%>").append("<option>TOM's Age</option>")
                        // set value
                        $("#<%=DropDownList2.ClientID%>").val("TOM's Age")
    
    
                    } else if ($(this).val().trim() == "Brando") {
                        $("#<%=DropDownList2.ClientID%>").empty()
                        $("#<%=DropDownList2.ClientID%>").append("<option>Brando's Age</option>")
                        $("#<%=DropDownList2.ClientID%>").val("Brando's Age")
    
                    } else if ($(this).val().trim() == "JACK")
                    {
                         $("#<%=DropDownList2.ClientID%>").empty()
                        $("#<%=DropDownList2.ClientID%>").append("<option>JACK's Age</option>")
                        $("#<%=DropDownList2.ClientID%>").val("JACK's Age")
                    }
    
    
                })
    
    
            })
    
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                Name:<asp:DropDownList ID="DropDownList1" runat="server">
                    <asp:ListItem>
                        JACK
                    </asp:ListItem>
                    <asp:ListItem>
                        TOM
                    </asp:ListItem>
                    <asp:ListItem>
                        Brando
                    </asp:ListItem>
                     </asp:DropDownList><br/>
                Age:<asp:DropDownList ID="DropDownList2" runat="server">
                   
                    
                     </asp:DropDownList>
    
            </div>
        </form>
    </body>
    </html>
    

    You could see as below:

    Best Regards

    Wei

    Wednesday, April 3, 2019 2:23 AM
  • User438705957 posted

    I handle the situation by setting up control parameters in the sqlDataSource for each dropdown.
    The value of the parent dropdown becomes the value of the parameter in a child data source.

    <asp:SqlDataSource ID="sdsPortfolios" runat="server" ConnectionString="<%$ ConnectionStrings:CS_Staffing %>"
            SelectCommand="SELECT Distinct Lookup_Unit_Codes.Unit_Code, Unit_Abbrv
                FROM Lookup_Unit_Codes 
                WHERE Unit_ADB = 'True' and Unit_Cluster = @Unit_Cluster
                and CARP_Portfolio = 'true' 
                ORDER BY Unit_Abbrv"
            OnSelected="sdsPortfolios_Selected">
            <SelectParameters>
                <asp:ControlParameter ControlID="drpCluster" Name="Unit_Cluster" PropertyName="SelectedValue" />
            </SelectParameters>
        </asp:SqlDataSource>
    
        <asp:SqlDataSource ID="sdsStreams" runat="server" ConnectionString="<%$ ConnectionStrings:CS_Staffing %>"
            SelectCommand="SELECT Distinct Lookup_Stream.Stream_Id, Stream 
                FROM Lookup_Stream 
                WHERE CARP_Unit_Code = @Unit_Code AND active = 'true'   
                ORDER BY Stream"
            OnSelected="sdsStreams_Selected">
            <SelectParameters>
                <asp:ControlParameter ControlID="drpPortfolios" Name="Unit_Code" PropertyName="SelectedValue" />
            </SelectParameters>
        </asp:SqlDataSource>

    In the above, the value of dropdown control drpPortfolios is given the name "Unit_Code".
    This is passed as a parameter to the data source sdsStreams in the SelectParameters.  It is then used in the select command.

    Wednesday, April 3, 2019 4:06 AM