Archive

Posts Tagged ‘ASP.NET’

ASP.NET AJAX AutoCompleteExtender not working

January 19th, 2010 joelhainley 2 comments

I was working on some new features for a client recently and they had a need for an AutoCompleteExtender on one of their forms. I hadn’t used the AutoCompleteExtender yet so I read through the documentation, coded up a webservice, and added  the appropriate items to the page and ran the app. When I tried typing into the TextBox that the AutoCompleteExtender was pointed at, nothing happened. In this posting I will detail the things that I looked at, and try to put together an accurate guide to making sure you’re AutoComplete works. I tried some of the “tips” that are out there after I had things working and they don’t help. So hopefully this will be a bit more definitive and useful than what I came across.

The first thing that I looked at was the method signature of the webservice. The documentation indicates that you need to use one of the following method signatures :

public string[] GetCompletionList(string prefixText, int count) { … }

..OR..

public string[] GetCompletionList( string prefixText, int count, string contextKey) { … }

What the documentation doesn’t tell you is that the name/spelling/capitalization of the method signature must match EXACTLY. If you decide that you don’t like the camelcase on the prefixText and want to use prefixtext instead….IT WON’T WORK. So make sure you have those signature matching correctly. I have seen some indications on the internet that you should use a static method such as :

public static string[] GetCompletionList(string prefixText, int count) { … } // !!! DON”T DO THIS !!!

DON’T DO THIS. It doesn’t work. Only the exact methods defined in the documentation and shown above will work.

Back to my project, I get these methods defined properly, compile the project, go to my textbox start to type and still I get NOTHING. So I look over the documentation and see some information about the class/method attributes that must be added. The documentation shows the following :

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public string[] GetCompletionList(
string prefixText, int count) { … }

What they don’t show in the documentation is that you need to also add the following to the class definition

[System.Web.Script.Services.ScriptService]

So I make these changes and run the application again, and STILL it doesn’t work. I’m stumped. I put a break at the webservice method and run again and it’s not even hitting the webservice. After a little bit of digging I come across Adding ASP.NET AJAX Configuration Elements to an Existing Web Site and a lightbulb goes on. This project was not setup as an AJAX.NET site, in fact it predates ASP.NET AJAX by a couple of years. Sure enough as I start to dig through the documentation I notice mappings to handlers for the webservice calls. So I follow the changes specified and run the site and it worked!

It took me a while to get this figured out, and I suspect there might be a few others out there that might have some problems getting this working. Below is a complete class that you can use as a reference. I haven’t included any of the code defining the AutoCompleteExtender because the documentation seems sufficient and I didn’t have any problems. There is also a video on the ASP.NET AJAX site that walks you through configuring your first AutoCompleteExtender and does a good job, as long as your site is configured properly!

Good luck!

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using DALInterface;
using CGWeb.framework;

namespace CGWeb.webservices {
///


/// Summary description for NewDXCompletion
///

///

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class NewDXCompletion : System.Web.Services.WebService {

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public string[] GetCompletionList(string prefixText, int count) { … }
}
}