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 15, 2018, 8:45 PM EST
Thanks, lot of helpfull for everybody
Nagi reddy KandiPosted May 4, 2017, 9:39 PM EDT
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, 2:24 AM EST
I used your code exactly as you told, but the auto complete does not work, do you have any hint?
santosh bhagatPosted Jun 4, 2012, 8:19 PM EDT
How to use this in share point 2010,kindly upload this as web part
justin kingPosted Oct 19, 2011, 10:48 PM EDT
Think you For Your code
girdhari prashaPosted Oct 5, 2011, 8:22 AM EDT
Hi,, [WebMethod] public string[] GetCountriesList(string prefixText) {
irene bernardPosted Sep 21, 2011, 11:16 PM EDT
thanks for share
kranthi kumarPosted Jul 14, 2011, 5:39 AM EDT
U gave to me what i need. Thank you very much
kbjj kjnbkPosted Dec 25, 2010, 1:28 PM EST
i have a problem in this code. public string[] GetCountriesList(string prefixText) don't working.... can you help me?
Enis ozturkPosted Dec 1, 2010, 3:48 AM EST
thanks for share,
praveen brittoPosted Nov 30, 2010, 9:28 PM EST
i m learning .net it s so help ful and easy to understand thnks lot
varun sPosted Oct 27, 2010, 3:27 AM EDT
How to retrieve data from xml file for autocomplete extender?
Maulik PatelPosted May 5, 2010, 4:07 AM EDT
this document help me lot. thank you very much.
Vadim KinPosted Mar 25, 2010, 11:51 AM EDT
Better register the assembly in Web.config. A bunch of extenders will not work correctly if registering on the page.
Nguyen Nam HaiPosted Mar 11, 2010, 10:44 PM EST
Code is very good ! Thank you very much
andres pPosted Feb 23, 2010, 1:21 PM EST
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 16, 2010, 9:32 PM EST
Hi I am get when numeric value is undefined ajaxautocomplete.
AKM HudaPosted Jan 3, 2010, 10:47 AM EST
Thank You For Your Code.
jose paisPosted Jan 2, 2010, 4:08 PM EST
wich version of AjaxControlToolkit do you use in this project?
bala kPosted Dec 9, 2009, 12:39 AM EST
"'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, 1:03 AM EDT
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, 5:25 AM EDT
merciiiiiiiiiiiiiiiiii grand saluationnn avous et atoussssssssss
nic peiPosted Jun 16, 2009, 10:31 PM EDT
THanks for your sharing
omar joundPosted Nov 8, 2007, 7:56 PM EST
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