Creating a web service for the AJAX Control Toolkit’s AutoCompleteExtender, using the Entity Framework (C#)
(Originally coded in VS2008 using c#, .net framework 3.5, compatibility with VS2005/.net 2.0 unknown)
In my last post, I showed you how to use javascript and asp hiddenField controls to save values selected in an AJAX AutoCompleteExtender.
In this article, we’ll take a closer look at the actual web service being called. The “GetPassengerList” method queries our Passengers entity set (ado.net Entity Framework) to return autocomplete options as a user types a passenger name into the textbox.
The code is pretty straightforward, and presented here:
[System.Web.Services.WebMethod] [System.Web.Script.Services.ScriptMethod] public string[] GetPassengerList(string prefixText, int count) { /********************************************************** * Takes a prefix string from a textbox and retrieves * passengers from the entitySet. Used by the AJAX Toolkit * AutoCompleteExtender. ***********************************************************/ List<string> Passengers = new List<string>(); //query the passenger entity: using (BusLineEntities myEnts = new BusLineEntities()) { IQueryable<Passenger> passengersQuery = from p in myEnts.Passengers where p.DisplayName.Contains(prefixText) orderby p.LastName select p; //add the values to the list and include id foreach (Passenger p in passengersQuery) Passengers.Add(AjaxControlToolkit.AutoCompleteExtender. _ CreateAutoCompleteItem(p.DisplayName, p.Id.ToString())); } return Passengers.ToArray(); }//end GetPassengerList
Note that we’re creating objects of type “AutoCompleteItem,” which enables us to have the dataText and an associated dataValue, much like we were using a databound dropDownList.
Now, if we had information we wanted to display in our autocomplete in addition to the person’s DisplayName, we could simply include the navigation path in our query and then append the desired value to our string:
[System.Web.Services.WebMethod] [System.Web.Script.Services.ScriptMethod] public string[] GetPassengerList(string prefixText, int count) { /********************************************************** * Takes a prefix string from a textbox and retrieves * passengers from the entitySet. Used by the AJAX Toolkit * AutoCompleteExtender. ***********************************************************/ List<string> Passengers = new List<string>(); //query the passenger entity: using (BusLineEntities myEnts = new BusLineEntities()) { IQueryable<Passenger> passengersQuery = from p in myEnts.Passengers.Include("States") where p.DisplayName.Contains(prefixText) orderby p.LastName select p; //add the values to the list and include id foreach (Passenger p in passengersQuery) Passengers.Add(AjaxControlToolkit.AutoCompleteExtender. _ CreateAutoCompleteItem(p.DisplayName + _ " (" + p.States.Abbrev + ")", p.Id.ToString())); } return Passengers.ToArray(); }//end GetPassengerList
And that’s it. The last sample assumes a NavigationPath of “States” leads to a State entity, and we want to display the user’s home state abbreviation next to their name in our autoCompleteExtender.








[...] how we’ll access our value (in this case, the person.Id). I’ll write a more in-depth article on that [...]
Leave your response!