Here is the solution.
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" tagprefix="ajaxToolkit"%>
Which says that the reference assembly "AjaxControlToolkit.dll" should be registered on the page by using above method. We should also have tagprefix="ajaxToolkit" which is similar to <ajaxToolkit: AutoCompleteExtender>.
In this article I have a very simple database table called "Tbl_Countries" with fields as:
CountryId int primary key,
CountryName varchar(50)
I am using a web service called "AutoComplete.asmx" whose code behind gets automatically added to the "App_Code" folder.
Note: A very important part is you need to give a reference of "AjaxControlToolkit.dll".
Here is complete code for "AutoComplete.asms.cs".
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// Summary description for AutoComplete
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService
{
[WebMethod]
public string[] GetCountriesList(string prefixText)
{
DataSet dtst = new DataSet();
SqlConnection sqlCon = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
string strSql = "SELECT CountryName FROM Tbl_Countries WHERE CountryName LIKE '" + prefixText + "%' ";
SqlCommand sqlComd = new SqlCommand(strSql, sqlCon);
sqlCon.Open();
SqlDataAdapter sqlAdpt = new SqlDataAdapter();
sqlAdpt.SelectCommand = sqlComd;
sqlAdpt.Fill(dtst);
string[] cntName = new string[dtst.Tables[0].Rows.Count];
int i = 0;
try
{
foreach (DataRow rdr in dtst.Tables[0].Rows)
{
cntName.SetValue(rdr["CountryName"].ToString(), i);
i++;
}
}
catch { }
finally
{
sqlCon.Close();
}
return cntName;
}
}
Let us take another page called "default.aspx" in which there is a <ScriptManager> tag containing a <Services> tag for specifying the path of webservices. You have already registered Assembly="AjaxControlToolkit" on this page so you can use that.
So my entire code will look something like this
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" tagprefix="ajaxToolkit"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="AutoComplete.asmx" />
</Services>
</asp:ScriptManager>
<div>
<asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender runat="server" ID="autoComplete1" TargetControlID="txtCountry" ServicePath="AutoComplete.asmx" ServiceMethod="GetCountriesList" MinimumPrefixLength="1" EnableCaching="true" />
</div>
</form>
</body>
</html>
Following snapshot gives you a clear idea of how the AutoCompleteExtender works.


Ravindra WaghmodePosted Feb 16, 2018, 1:45 AM
Thanks, lot of helpfull for everybody
Nagi reddy KandiPosted May 5, 2017, 1:39 AM
Dear All, Please this code is working fine.Please remove the comment of this code "[System.Web.Script.Services.ScriptService]" in webservice.
mahtab asadiPosted Dec 11, 2012, 7:24 AM
I used your code exactly as you told, but the auto complete does not work, do you have any hint?
santosh bhagatPosted Jun 5, 2012, 12:19 AM
How to use this in share point 2010,kindly upload this as web part
justin kingPosted Oct 20, 2011, 2:48 AM
Think you For Your code
girdhari prashaPosted Oct 5, 2011, 12:22 PM
Hi,, [WebMethod] public string[] GetCountriesList(string prefixText) {
irene bernardPosted Sep 22, 2011, 3:16 AM
thanks for share
kranthi kumarPosted Jul 14, 2011, 9:39 AM
U gave to me what i need. Thank you very much
kbjj kjnbkPosted Dec 25, 2010, 6:28 PM
i have a problem in this code. public string[] GetCountriesList(string prefixText) don't working.... can you help me?
Enis ozturkPosted Dec 1, 2010, 8:48 AM
thanks for share,
praveen brittoPosted Dec 1, 2010, 2:28 AM
i m learning .net it s so help ful and easy to understand thnks lot
varun sPosted Oct 27, 2010, 7:27 AM
How to retrieve data from xml file for autocomplete extender?
Maulik PatelPosted May 5, 2010, 8:07 AM
this document help me lot. thank you very much.
Vadim KinPosted Mar 25, 2010, 3:51 PM
Better register the assembly in Web.config. A bunch of extenders will not work correctly if registering on the page.
Nguyen Nam HaiPosted Mar 12, 2010, 3:44 AM
Code is very good ! Thank you very much
andres pPosted Feb 23, 2010, 6:21 PM
hello my name is andres'm following your tutorial AutoCompleteExtender, I have a problem because CompletionSetCount = "10" did not work .. could help me thanks my e-mail: [email protected] code is: [WebMethod] public string[] GetAutocompleteList(string prefixText) { DataSet dtst = new DataSet(); SqlConnection sqlCon = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]); string strSql = "SELECT titulo FROM PDF WHERE titulo LIKE '" + prefixText + "%'"; SqlCommand sqlComd = new SqlCommand(strSql, sqlCon); sqlCon.Open(); SqlDataAdapter sqlAdpt = new SqlDataAdapter(); sqlAdpt.SelectCommand = sqlComd; sqlAdpt.Fill(dtst); string[] cntName = new string[dtst.Tables[0].Rows.Count]; int i = 0; try { foreach (DataRow rdr in dtst.Tables[0].Rows) { cntName.SetValue(rdr["titulo"].ToString(), i); i++; } } catch { } finally { sqlCon.Close(); } return cntName; }
yagya gautamPosted Feb 17, 2010, 2:32 AM
Hi I am get when numeric value is undefined ajaxautocomplete.
AKM HudaPosted Jan 3, 2010, 3:47 PM
Thank You For Your Code.
jose paisPosted Jan 2, 2010, 9:08 PM
wich version of AjaxControlToolkit do you use in this project?
bala kPosted Dec 9, 2009, 5:39 AM
"'AutoCompleteExtender' is not known element ,this can occur if there is compilation error in the website ,or web.config file is missing"-- like that error message i'm getting sir, please let me know the solution!!!!
gulam ansariPosted Aug 17, 2009, 5:03 AM
hi i am using vs 2005 to run this concept but i found a error Error 1 Could not load file or assembly 'System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. F:\dfggfgf\exfgfg\AJAXAutoComplete\Web.config 29 i have added the assembly reference but it is not working please help me any one as soon as possible...?
saidPosted Aug 8, 2009, 9:25 AM
merciiiiiiiiiiiiiiiiii grand saluationnn avous et atoussssssssss
nic peiPosted Jun 17, 2009, 2:31 AM
THanks for your sharing
omar joundPosted Nov 9, 2007, 12:56 AM
Dear Brother Munir. Im using asp.net 2.0 and trying to set up a google earth using c# using Ajax guide book. but when I created a folder they ask me to add anthem.DLL which is doesn't exist in the directory. please how to install anthem.dll into visual studio 2005. I did extract anthem.net zip file but couldn't find nothing. Thank you so much