locked
Change selection choices of a droplist depending on the selection of another droplist. RRS feed

  • Question

  • User982203039 posted

    Can I change the selection of a second drop down list depending on the first selected dropdown value?

    In the code below if the listDepartments is "Sales" I only want the choice for listCatagory droplist to be Customers, and when selecting HR I only want the choice to be "Resumes" Is this possible with the code below or do I need a new approach?

    List<SelectListItem> listDepartments = new List<SelectListItem>();
    
    
    
                                listDepartments.Add(new SelectListItem
                            {
                                Text = "Sales",
                                Value = "Sales",
    
                            });
                                listDepartments.Add(new SelectListItem
                            {
                                Text = "HR",
                                Value = "HR"
                            });
    
    
    
                            List<SelectListItem> listCatagory = new List<SelectListItem>();
                                listCatagory.Add(new SelectListItem
                            {
                                Text = "Customers",
                                Value = "Customers",
    
                            });
                            listCatagory.Add(new SelectListItem
                            {
                                Text = "Resumes",
                                Value = "Resumes",
                            });



    Thursday, July 25, 2019 2:14 PM

All replies

  • User288213138 posted

    Hi Baze72,

    According to your description, here is a demo for your reference.

    I through switch case to judge Selectvalues of listDepartments . Then bind data to listCatagory as required.

    The code:

    <div>        
                <asp:DropDownList ID="listDepartments" runat="server" AutoPostBack="true" OnSelectedIndexChanged="listDepartments_SelectedIndexChanged">
                    <asp:ListItem>Select an Item</asp:ListItem>
                    <asp:ListItem>Sales</asp:ListItem>
                    <asp:ListItem>HR</asp:ListItem>
                </asp:DropDownList>
                <asp:DropDownList ID="listCatagory" runat="server"></asp:DropDownList>
            </div>
    protected void listDepartments_SelectedIndexChanged(object sender, EventArgs e)
            {
                switch (listDepartments.SelectedValue)
                {
                    case "Sales":
                        listCatagory.DataSource = new List<string>() { "Customer1", "Customer2", "Customer3" };
                        listCatagory.DataBind();
                        break;
                    case "HR":
                        listCatagory.DataSource = new List<string>() { "Resume1", "Resume2", "Resume3" };
                        listCatagory.DataBind();
                        break;
    
                    default:
                        listCatagory.DataSource = new List<string>();
                        listCatagory.DataBind();
                        break;
                }
            }
    

    The result:

    Best regards,

    sam

    Friday, July 26, 2019 4:13 AM