ASP.NET AJAX AutoCompleteExtender control

 

Asp.Net Ajax providing the AutoCompleteExtender control to auto populate the values for asp.net textbox control. We have to provide web method as input for AutoCompleteExtender control through the web service or through public method.

 

In this article we discuss about how to auto populate the values by using AutoCompleteExtender control through the public web methods.

 

AutoCompleteExtender control has several properties as shown below.

 

ServiceMethod="GetNames"  => code behind public web method

MinimumPrefixLength="1"       => minimum length text required to auto populate the values

CompletionInterval="100"            => how much fast you want populate the values

EnableCaching="false"                   => for caching the searches (autocomplete)

CompletionSetCount="10"             => Number of values you want to show as auto populate

TargetControlID="txtContactsSearch" =>the textbox for which you want to set the autocompleteextender.

FirstRowSelected = "false" set true if you want select first value by default

 

Now add reference for AjaxControlToolkit and add AutoCompleteExtender control to your default.aspx page as shown below.

 

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:ToolkitScriptManager ID="ScriptManager1" runat="server" />

        <asp:TextBox ID="txtNameSearch" runat="server"></asp:TextBox>

       

        <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="txtNameSearch"

            MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="1" CompletionInterval="1000" ServiceMethod="GetNames">

         </asp:AutoCompleteExtender> 

    </div>

    </form>

</body>

</html>

 

Then add GetNames() public method to your code behind file as shown below.

 

using System;

using System.Collections.Generic;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

 

    [System.Web.Script.Services.ScriptMethod()]

    [System.Web.Services.WebMethod]

    public static List<string> GetNames(string prefixText)

    {

        List<string> listNames = new List<string>();

        listNames.Add("Raj");

        listNames.Add("John");

        listNames.Add("Bob");

        listNames.Add("Bill Gates");

        listNames.Add("Smith");

 

        return listNames;

    }

}

 

If you run the application the output is as shown below.

 

                              AspNetAutocomplete.zip (1.06 mb)