The Atlas AutoCompleteExtender

by Christian Wenz

Related link: http://www.hauser-wenz.de/s9y/index.php?/archives/153-The-Atlas-AutoCompleteExte…



One of the most anticipated new features of the Atlas December CTP is the AutoCompleteExtender which adds a feature like Google Suggests to a web page. Nikhil Kothari blogged about the new release, but I did not find a complete step-by-step example (although I honestly did not look very long for it). So I sat down and played around with it and found out how this feature works.
The first step is quite easy: Take the <atlas:AutoCompleteExtender> control and put it in your code. Link it to an existing web control you would like to "auto-complete", ideally a textbox control. Then, link these two controls using the TargetControlID attribute and provide the URL to a web service and the name of a web method within it:



<asp:TextBox ID="vendor" runat="server"></asp:TextBox>

<input type="button" value="Search" />


<atlas:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server">

  <atlas:AutoCompleteProperties Enabled="true" ServicePath="AutoCompleteSample.asmx"

    ServiceMethod="GetValues" TargetControlID="vendor" />

</atlas:AutoCompleteExtender>



On the server side, you have to write this web method. The only difficulty is to find out the method signature. Well, here it is:



public string[] MethodName(string PrefixText, int count) {}



Obviously, PrefixText is the string to search for, and count the maximum number of matches to be returned and displayed. It is also important to know that only .NET Web Sevices are supported


This being said, it is very easy to write a little lookup code. You could query a database, or you just use a static dictionary as in the example below. I know that there are numerous possibilities to optimize the code, but hey, it works :-)



<%@ WebService Language="C#" Class="AutoCompleteSampleService" %>



using System;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;



[WebService(Namespace = "http://hauser-wenz.de/")]


[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class AutoCompleteSampleService : System.Web.Services.WebService

{



  [WebMethod]

  public string[] GetValues(string PrefixText, int count)

  {

    string[] values = { "incomplete", "inconsistent", "incompatible", "impossible", "important", "increment", "include", "impose" };

    int matches = 0;


    for (int i = 0; i < values.Length; i++)

    {

      if (values[i].StartsWith(PrefixText))

      {

        matches++;

      }

    }




    int size = Math.Min(matches, count);

    string[] output = new string[size];

    for (int i = 0; i < values.Length; i++)

    {

      if (size == 0)

      {

        break;

      }


      if (values[i].StartsWith(PrefixText))

      {

        output[--size] = values[i];

      }

    }

    return output;

  }



}




And that's it! Atlas generates script code to query the web service after at least three characters have been entered into the text field.

5 Comments

aldone
2006-03-13 14:58:56
Please help me
Hello I'm from Peru and have some problem whit this control, I don't know why.Really es very simple buttttt....


Well the problem is de first parameter of de signature into the server code of the web service,when i debug this de first parameter has the value of null :S and the suggest reported is all rows of my table


Some suggest???


Sorry for my poor english

wenz
2006-03-15 00:14:55
works for me ...
do you have __simplified__ code one could have a look at (both ASP.NET source and the actual page)?

2006-05-22 20:00:00
Yeah! a working sample! Thanks very much.
Dave Reed
2006-05-28 16:14:49
Just for fun I created a custom extender utilizing the built in one that lets you do auto completion without needing a web service (but it supports that too). :)
Infinities Loop
Christian Wenz
2006-05-29 00:05:44
Very interesting extender, Dave! Thanks for sharing. For those interested, here is the permalink for Dave's entry.