dropdown2 to show the filtered result from table on selecting value from the dropdownlist1

Viewed 34

in my DB table i have a column Type which has two categories Furniture and Electronics which is datasource of first dropdownlist. The other column is of name. I want my second dropdown to display all the names which are of type furniture and electronics names when i select electronics from first dropdown i am using entityframework, here is the code

 protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<string> nameasset;
            
            if (DropDownList3.SelectedValue == "Furniture")
            {
                //Response.Write(DropDownList3.SelectedValue);
                nameasset = db.tbl_asset.Where(x => x.asset_type == "Furniture").Select(x => x.asset_name).ToList();
                DropDownList4.DataSource = nameasset;
                DropDownList4.DataBind();
            }
            else if(DropDownList3.SelectedValue == "Electronics")
            {
                //Response.Write(DropDownList3.SelectedValue);

                nameasset = db.tbl_asset.Where(x => x.asset_type == "Electronics").Select(x => x.asset_name).ToList();
                DropDownList4.DataSource = nameasset;
                DropDownList4.DataBind();
            }
            else
            {

            }
            
        } 

also when i am selecting the furniture option from the dropdown it is by default selecting electronics over and over again and just displaying electronics items name from data base

1 Answers

Add this in the page_load event. Please make sure to bind dropdownlist3 inside IsPostBack.

if(!IsPostBack)
{
    DropDownList3.DataSource = [AddDatasource];
    DropDownList3.DataBind();
}
Related