Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

ASP.NET Ajax AutoCompleteExtender Without Using Web Service

4.76/5 (12 votes)
29 Jun 2012CPOL 73K  
ASP.Net Ajax AutoCompleteExtender without using Web Service

Introduction

This tip shows how to use Ajax AutoCompleteExtender in an ASP.NET application, without adding a Web Service.

Steps

  • Create an ASP.NET 4.0 Web Application
  • Add reference to AjaxControlToolit
  • Open Default.aspx
  • Add Script Manager
  • Add Ajax AutoCompleteExtender 

Using the Code

Using the code (default.aspx)

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
<asp:ScriptManager ID="ScriptManager1" runat="server" 
EnablePageMethods = "true">
</asp:ScriptManager>

Enter a Product Name
<asp:TextBox ID="txtProductList" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ServiceMethod="GetProductList" 
    MinimumPrefixLength="1"
    CompletionInterval="0" EnableCaching="false" CompletionSetCount="10" 
    TargetControlID="txtProductList"
    ID="autoCompleteExtender1" runat="server" FirstRowSelected = "false">
</cc1:AutoCompleteExtender>
    </div>
    </form>
</body>
</html>

Default.aspx.cs

C#
[System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static string[] GetProductList(string prefixText, int count)
    {
        // Get the Products From Data Source. Change this method to use Database
        List<string> productList = GetProducts();

        // Find All Matching Products
        var list = from p in productList
                   where p.Contains(prefixText)
                   select p;

        //Convert to Array as We need to return Array
        string[] prefixTextArray = list.ToArray<string>();

        //Return Selected Products
        return prefixTextArray;
    }  

Points of Interest

  • If you want that your method responds to the Scripting Language, make sure to add [System.Web.Script.Services.ScriptMethod()] to method attribute. Else, this won't work
  • And yes, this does not require addition of Web Service!

History

  • 29th June, 2012: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)