ASP.NET: AJAX Cascading Dropdownlist with Autosuggest using Jquery
DEMO Cascading dropdownlists are a set of dropdownlist controls in which each dropdownlist is populated based on another dropdownlist...

https://www.programming-free.com/2013/05/aspnet-ajax-cascading-dropdownlist-with.html
Cascading dropdownlists are a set of dropdownlist controls in which each dropdownlist is populated based on another dropdownlist's selected item and the best approach to implement this is without refreshing the whole page in AJAX way. There are several ways in which we can achieve this such as using AJAX Toolkit's CascadingDropdown extender along with ASP.NET dropdownlist, using dropdownlist in an UpdatePanel and using jquery to make AJAX calls to populate dropdownlist. One more feature that is desirable in a dropdownlist is the autocomplete functionality, that can help the user to get to the right option without scrolling much especially when there are lot of items in the dropdownlist.
In this post, I am going to explain with an example on how to implement cascading dropdownlist using jquery and apply autocomplete feature using Select2 jquery plugin in the client side. We are going to populate the dropdownlists by making jquery ajax calls to server side method(WebMethod) and enable autocomplete feature in client side using Select2 jquery plugin.
1. First of all let us download all the required libraries,
a. jQuery
b. Select2
2. Place the above downloaded libraries inside your asp.net project folder.
3. Let us now create two model classes, that would serve the data required for the dropdownlists. The first dropdownlist will contain items that are departments and the other will contain items that are subjects corresponding to the department that is selected in the first dropdownlist. Department dropdownlist will be populated on page load and subjects dropdownlist will be populated whenever an item is selected in the department dropdownlist by making ajax calls to the server.
Department.cs
Subjects.cs
Department.cs
namespace CascadingAutoCompleteSample { public class Department { public string dptName { get; set; } public string dptAliasName { get; set; } public List<Department> GetDepartment() { return new List<Department> { new Department { dptName = "Aeronautical Engineering", dptAliasName = "AE" }, new Department { dptName = "Biomedical Engineering", dptAliasName = "BME" }, new Department { dptName = "Computer Science Engineering", dptAliasName = "CSE" }, new Department{ dptName = "Information Technology", dptAliasName = "IT" }, new Department{ dptName = "Mechanical Engineering", dptAliasName = "MECH" }, new Department{ dptName = "Electronics and Communication Engineering", dptAliasName = "ECE" } }.ToList(); } } }
Subjects.cs
namespace CascadingAutoCompleteSample { public class Subjects { public string deptAliasName { get; set; } public string subName { get; set; } public string subAliasName { get; set; } public List<Subjects> GetSubjects() { return new List<Subjects> { new Subjects { deptAliasName="AE", subName = "Fluid Mechanics", subAliasName = "FM" }, new Subjects { deptAliasName="AE", subName = "Aircraft System", subAliasName = "AS" }, new Subjects { deptAliasName="AE", subName = "Helicopter Theory", subAliasName = "HT" }, new Subjects { deptAliasName="BME", subName = "Bio Analytical Techniques", subAliasName = "BAT" }, new Subjects { deptAliasName="BME", subName = "Bio Signal Processing", subAliasName = "BSP" }, new Subjects { deptAliasName="CSE", subName = "Data Structures & Algorithms", subAliasName = "DSA" }, new Subjects{ deptAliasName="CSE", subName = "Computer Architecture", subAliasName = "CO" }, new Subjects{ deptAliasName="CSE", subName = "Artificial Intelligence", subAliasName = "AI" }, new Subjects{ deptAliasName="CSE", subName = "Fundamentals of Computing", subAliasName = "FOC" }, new Subjects{ deptAliasName="IT", subName = "Numerical Methods", subAliasName = "NM" }, new Subjects{ deptAliasName="IT", subName = "Fundamentals of Computing", subAliasName = "FOC" }, new Subjects{ deptAliasName="IT", subName="Priciples of Communication", subAliasName="POC" }, new Subjects{ deptAliasName="MECH", subName = "Measurements and Controls", subAliasName = "MC" }, new Subjects{ deptAliasName="MECH", subName = "Machine Tools", subAliasName = "MT" }, new Subjects{ deptAliasName="ECE", subName = "Electronic Devices & Circuits", subAliasName = "EDC" }, new Subjects{ deptAliasName="ECE", subName = "Electro Magnetic Theory", subAliasName = "EMT" } }.ToList(); } } }
Data is hard coded in these classes, you can also modify this to fetch items from a database if you prefer.
4. Let us create an aspx page with two dropdownlists and script to make ajax calls to server to populate one of it based on the other. The script will also convert these dropdownlists in to rich dropdownlists that enables the user to search for an item in it.
4. Let us create an aspx page with two dropdownlists and script to make ajax calls to server to populate one of it based on the other. The script will also convert these dropdownlists in to rich dropdownlists that enables the user to search for an item in it.
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Cascading AutoComplete DropDownlist using jQuery</title> <link href="Styles/select2.css" rel="stylesheet" /> <script src="Scripts/jquery-1.9.1.js"></script> <script src="Scripts/select2.js"></script> <script type="text/javascript"> $(function () { var $ddl = $("select[name$=ddlDepartment]"); $ddl.select2(); var $ddlSub = $("select[name$=ddlSubject]"); $ddl.focus(); $ddl.bind("change keyup", function () { loadSubjects($("select option:selected").val()); $ddlSub.fadeIn("slow"); }); }); function loadSubjects(selectedItem) { $.ajax({ type: "POST", url: "CascadingAutocomplete.aspx/GetSubjectList", data: "{deptAliasName:'" + selectedItem + "'}", contentType: "application/json; charset=utf-8", dataType: "json", async: true, success: function (data) { printSub(data.d); }, error: function (xhr, status, err) { var err = eval("(" + xhr.responseText + ")"); alert(err.Message); } }); } function printSub(data) { $("select[name*=ddlSubject] > option").remove(); $("select[name*=ddlSubject]").append( $("<option></option>").val("Select Subject").html("Select Subject") ); for (var i = 0; i < data.length; i++) { $("select[name*=ddlSubject]").append( $("<option></option>").val(data[i].subAliasName).html(data[i].subName) ); } $("#subjectdiv").css("display","block"); $("select[name$=ddlSubject]").select2(); } </script> </head> <body> <form id="form1" runat="server"> <div style="text-align:center"> <h1>Cascading AutoComplete in ASP.NET using jQuery</h1> Select Department : <asp:DropDownList ID="ddlDepartment" runat="server" Width="280px"> </asp:DropDownList> <br/> Select Subject : <asp:DropDownList ID="ddlSubject" runat="server" Width="280px" > </asp:DropDownList> </div> </form> </body> </html>
Let us take a close look on the jquery script that makes ajax calls to populate the second dropdownlist. The loadSubjects method is fired whenever an user selects or changes value in the first dropdownlist and makes ajax call to method that is declared as WebMethod in the code behind page(CascadingAutocomplete.aspx/GetSubjectList). Once the data is fetched from server side, printSub method in the above script updates user interface in this case the second dropdownlist with the response. Both the dropdownlists are enabled with autosuggest feature using select2 plugin.
5. Now in the code behind page let us write code that is called by the jquery script to fetch data for the Subjects dropdownlist. Note that to call a server side method, the method should be declared as a WebMethod.
That is all. See below screenshots or try the live demo to know how the outcome of the above implementation will look like,
5. Now in the code behind page let us write code that is called by the jquery script to fetch data for the Subjects dropdownlist. Note that to call a server side method, the method should be declared as a WebMethod.
using System.Web.Services; using System.Web.Script.Services; namespace CascadingAutoCompleteSample { public partial class CascadingAutocomplete : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Department dept = new Department(); List<Department> lstDept = dept.GetDepartment(); ddlDepartment.DataSource = lstDept; ddlDepartment.DataTextField = "dptName"; ddlDepartment.DataValueField = "dptAliasName"; ddlDepartment.DataBind(); ddlDepartment.Items.Insert(0, new ListItem("Select Department", "Select Department")); ddlDepartment.SelectedItem.Text = "Select Department"; } [WebMethod] [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)] public static List<Subjects> GetSubjectList(string deptAliasName) { Subjects sub = new Subjects(); List<Subjects> lstSubs = sub.GetSubjects().Where(x => x.deptAliasName == deptAliasName).ToList(); return lstSubs; } } }
That is all. See below screenshots or try the live demo to know how the outcome of the above implementation will look like,
Please
leave your comments and queries about this post in the comment sections
in order for me to improve my writing skills and to showcase more
useful posts. Thanks for reading!!
>
Very Nice and useful article.Keep going
ReplyDeleteThank you!
Deletegood stuff. nicely done too.
ReplyDeleteThank you!
DeleteGood work Priya Darshini
ReplyDeleteThank you!
Deletevery good initiative from you.
ReplyDeleteThank you!
DeleteHi Priya
ReplyDeleteI try to used the above in of my page which included in master page, In that I am using jquery treeview plugin, I am facing some problems while implementing the selects().
Can you please help regarding this.
Thanks
Prakash
Please explain your problem and post your code at http://forums.asp.net for immediate help on this by experts.
DeleteHi Priya- if I like to capture my selection i.e. Department and the Subject after user has chosen both, how can I do that? I am trying to implement this example but the issue I am having is to bind the capture the change keyup (user's selection) for the last dropdown list.
ReplyDeleteThanks for your help in advance !
Or to put it simply, how should I print (alert) chosen Department and Subject on the screen every time I change the department and/or subject.
ReplyDeleteHi,
DeleteYou can edit the script in this example like this to throw an alert when the user selects a department or subject,
<script type="text/javascript">
$(function () {
var $ddl = $("select[name$=ddlDepartment]");
$ddl.select2();
var $ddlSub = $("select[name$=ddlSubject]");
$ddl.focus();
$ddl.bind("change keyup", function () {
loadSubjects($("select option:selected").val());
$ddlSub.fadeIn("slow");
});
//this alerts the selected subject
$ddlSub.bind("change keyup", function () {
alert($("select option:selected").val());
});
});
function loadSubjects(selectedItem) {
alert(selecteditem);// This alerts the selected department
$.ajax({
type: "POST",
url: "CascadingAutocomplete.aspx/GetSubjectList",
data: "{deptAliasName:'" + selectedItem + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
success: function (data) {
printSub(data.d);
},
error: function (xhr, status, err) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
}
function printSub(data) {
$("select[name*=ddlSubject] > option").remove();
$("select[name*=ddlSubject]").append(
$("<option></option>").val("Select Subject").html("Select Subject")
);
for (var i = 0; i < data.length; i++) {
$("select[name*=ddlSubject]").append(
$("<option></option>").val(data[i].subAliasName).html(data[i].subName)
);
}
$("#subjectdiv").css("display","block");
$("select[name$=ddlSubject]").select2();
}
</script>
Hope this helps!
Thanks,
Priya
Thanks for the quick response. I'll try it and let you know.
ReplyDelete//added these variables for lower levels criteria //
ReplyDeletevar selectedDeptNbr;
var selectedSubDeptNbr;
var selectedClassNbr;
var selectedSubClassNbr;
$(function () {
var $ddlDeptLst = $("select[name$=ddlDepartment]"); //dept
var $ddlSubDeptLst = $("select[name$=ddlSubDepartment]"); //subdept
var $ddlClassLst = $("select[name$=ddlClass]"); //class
var $ddlSubClassLst = $("select[names$=ddlSubClass]"); //subclass
$ddlDeptLst.select2();
$ddlDeptLst.focus();
$ddlDeptLst.bind("change keyup", function () {
$("#classdiv").css("display", "none");
$("#subclassdiv").css("display", "none");
selectedDeptNbr = $('#deptdiv option:selected').val();
loadSubDepts($('#deptdiv option:selected').val());
$ddlSubDeptLst.fadeIn("slow");
$ddlSubDeptLst.focus();
});
$ddlSubDeptLst.bind("change keyup", function () {
$("#subclassdiv").css("display", "none");
selectedSubDeptNbr = $('#subdeptdiv option:selected').val();
loadClasses($("#subdeptdiv option:selected").val());
$ddlClassLst.fadeIn("slow");
});
$ddlClassLst.bind("change keyup", function () {
selectedClassNbr = $('#classdiv option:selected').val();
loadSubClasses($("#classdiv option:selected").val());
alert("Your selection is :" + selectedDeptNbr + "-" + selectedSubDeptNbr + "-" + selectedClassNbr);
$ddlSubClassLst.fadeIn("slow");
});
$ddlSubClassLst.bind("change keyup", function () {
alert("Your selection is :" + selectedDeptNbr + "-" + selectedSubDeptNbr + "-" + selectedClassNbr + "-" + selectedSubClassNbr);
});
});
in the above example,everything works just fine but the last bind i.e. $ddlSubClassLst.bind does not even get called and it does not even reach to alert() call either.
ReplyDeleteBut my alert one step above i.e.
alert("Your selection is :" + selectedDeptNbr + "-" + selectedSubDeptNbr + "-" + selectedClassNbr);
works just find returned the value for dept,subdept and as well as selected class
$ddlSub.bind("change keyup", function () {
ReplyDeletealert($("select option:selected").val());
});
});
I think this bind never gets executed in your example or at least for me.
Very nice.Its very much useful.
ReplyDeleteLink to download source code?
ReplyDeleteIts great. work fine for me. very very useful article.
ReplyDeletein case of cascading dropdown the result is populating in 2 dropdowns instead of 1
ReplyDeletePlease Help How can i include this dropdownlist to my project
ReplyDeletec++ codes for beginners
ReplyDeleteLink to download code ?
ReplyDeleteNice information on .net
ReplyDeleteselenium training in Bangalore
selenium courses in Bangalore
selenium training in Marathahalli
selenium training institute in bangalore
best web development training in Bangalore
web development course in bangalore
best web development training in Bangalore
web development training in Marathahalli
techbindhu
I used to be able to find good advice from your articles.
ReplyDeletemobile service center banglore
Samsung Galaxy S8+ service center near me
oppo Find 7a authorized service center near me
oneplus 3 service center in whitefield
realme 2 service center in marathahalli
BECOME A DIGITAL MARKETING
ReplyDeleteEXPERT WITH US
COIM offers professional Digital Marketing Course Training in Delhi to help you for job and your business on the path to success.
+91-9717 419 413
8057555775
Digital Marketing Course in Laxmi Nagar
Digital Marketing Institute in Delhi
Digital Marketing training in Preet Vihar
Online Digital Marketing Course in India
Digital Marketing Institute in Delhi
Digital Marketing Institute in Delhi
Love Romantic
Digital Marketing Institute In Greater Noida
Digital Marketing Institute In Alpha greater noida
Thanks for sharing this blog
ReplyDelete[url=http://procinehub.com/]best baby photographer in jalandhar[/url]
[url=http://procinehub.com/]best fashion photographer in Chandigarh[/url]
[url=https://www.styleandgeek.com/home-remedies-hair-fall//]home remedies for hair fall[/url]
[url=https://www.styleandgeek.com/top-25-home-remedies-to-remove-tanning//home-remedies-hair-fall//]home remedies to get rid of tanning[/url]
[url=https://www.lms.coim.in//]Online Digital Marketing Training[/url]
Excellent post! We will be linking to this great article on our website. Keep up the good writing.
ReplyDeleteHuawei mobile LCD screen replacement in Bangalore
Vivo mobile LCD screen replacement in Bangalore
Motorola mobile LCD screen replacement in Bangalore
Asus mobile LCD screen replacement in Bangalore
LG Stylus 2 mobile LCD screen replacement in Bangalore
Thanks.
ReplyDeletedelhi to kasauli
manali tour package for couple
cheap honeymoon destinations outside india
distance between delhi to kasauli by road
tourist places in india for summer
holiday destinations near delhi
best tourist places in india
hill station tour packages
himachal tour package for couple
The most sacred place National War museum Delhi is inaugurated now in the nearby vicinity of India Gate . Here in the article we will help you out in solving our signature query how to reach National War memorial Delhi nearby India Gate . Along with that we will also sort out few other queries related to the National War memorial Delhi like nearest metro station, war memorial Delhi timing and also nearest metro stations to India Gate .
ReplyDeleteHey loved reading your blog, must say that I can fully relate. Would really appreciate if you could check out some of my work and leave a thoughtful comment. Thankyou. Hill station tour packages Hill station packages
ReplyDeleteI am very impressive to read this post thanks for sharing this very good information.
ReplyDeleteEvents Planner In Delhi
Wedding Planner In Delhi
Thanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles.
ReplyDeleteEvents Planner In Delhi
Wedding Planner In Delhi
Download Yoast SEO Premium WordPress Plugin
Download Yoast SEO Premium WordPress Plugin
Digital Marketing Institute in Delhi
Digital Marketing Course in Delhi
Thanks for posting
ReplyDeletemy website techwolk is related to technology
Thanks for posting
ReplyDeletemy webside techwolk.in/Sonsindiasong
I have Webside techwolk.in/Sonsindiasong for Related by All type Songs.
Thanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles.
ReplyDeleteHome Remedies for Tanning
how to remove tanning
best Car Accident Lawyer in Phoenix
How to donate a car in Maryland
best magazine in India
how to lose 10 kgs in one month
Great post You are sharing much information keep it up ..awesome post
ReplyDeleteI am sharing biography of top 10 businesses of india. Check my link for more informati
on.
Great post You are sharing much information keep it up ..awesome post
ReplyDeleteI am sharing <biography of top-10-richest-actor-in-india Check my link for more information.
This is great. Brother Printer Drivers. Thank you so much.
ReplyDeleteGreat info! I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have 먹튀검증 Hi. I think I have found you just the right time 먹튀검증
ReplyDelete
ReplyDeleteGood post thanks for share information.
AppValley VIP APK
Cami Elliott
lola Iolani Momoa
rolling paper alternatives
Mp3boo
free edu email
This comment has been removed by the author.
ReplyDeleteI got a web site from where I be capable of really obtain valuable information regarding my study and knowledge.
ReplyDeleteGreat Article… Good Job… Thanks For Sharing…
Website:오피사이트