SIGN UP MEMBER LOGIN:    
ARTICLE

AJAX AutoCompleteExtender

Posted by Munir Shaikh Articles | AJAX in C# October 04, 2007
In one of the Ajax project while using <ajaxToolkit: AutoCompleteExtender> I was getting following error: Error Creating control - autoComplete1. This control cannot be displayed because it's tag prefix is not registered in the web form.
Reader Level:
Download Files:
 

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.

Image1.GIF

Login to add your contents and source code to this article
share this article :
post comment
 

Think you For Your code

Posted by justin king Oct 20, 2011

Hi,, [WebMethod] public string[] GetCountriesList(string prefixText) {

Posted by girdhari prasha Oct 05, 2011

thanks for share

Posted by irene bernard Sep 22, 2011

U gave to me what i need. Thank you very much

Posted by kranthi kumar Jul 14, 2011

i have a problem in this code. public string[] GetCountriesList(string prefixText) don't working.... can you help me?

Posted by kbjj kjnbk Dec 25, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor