Home » .net, Entity Framework, Other Tech

Creating a web service for the AJAX Control Toolkit’s AutoCompleteExtender, using the Entity Framework (C#)

1 September 2009 One Comment

(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.

One Comment »

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.