Let's see how we can do this.
Step 1: Place two select and a div in the body tag.
Step 2: Place below javascript in the page to populate carmanufacturer and model dropdownlist
Before populating second dropdownlist, it needs to be cleared. $('#model option').remove(); "Select" option needs to be added to second dropdownlist prior to adding data. var selectOption = $(document.createElement('option')); $('#model').append(selectOption.val("0").html("Select")); We can pass data to webmethod like below data: "{'manufacturerid': " + $('#carmanufacturer').val() + "}", Step 3: Create a class Cars public class Cars{ public string manufcturerName; public int manufacturerid; public string manufacturerModel; } Step 4: Create a method GetCarManufacturer which will fill the first dropdownlist [WebMethod] public static List
new Cars{manufacturerModel = "C Class", manufacturerid= 1} ,
new Cars{manufacturerModel = "S Class", manufacturerid= 1} ,
new Cars{manufacturerModel = "E Class", manufacturerid= 1} ,
new Cars{manufacturerModel = "3 Series", manufacturerid= 2} ,
new Cars{manufacturerModel = "5 Series", manufacturerid= 2} ,
new Cars{manufacturerModel = "7 Series", manufacturerid= 2 } ,
new Cars{manufacturerModel = "A4", manufacturerid= 3} ,
new Cars{manufacturerModel = "A5", manufacturerid= 3} ,
new Cars{manufacturerModel = "Q5", manufacturerid= 3 } ,
new Cars{manufacturerModel = "Q7", manufacturerid= 3 } ,
new Cars{manufacturerModel = "Swift", manufacturerid= 4} ,
new Cars{manufacturerModel = "SX4", manufacturerid= 4} ,
new Cars{manufacturerModel = "Alto", manufacturerid= 4 } ,
new Cars{manufacturerModel = "Ritz", manufacturerid= 4}
};
return objCars;
}
Live Demo
This ends the article of cascading dropdownlist using jquery.
Like us if you find this post useful. Thanks!
chinmaya parija
DownLoad code here
Download Code
Download Code
No comments:
Post a Comment