Is AutoComplete Extender working

There are numerous blogs and forums discussing that “AutoComplete Extender is not working”. When I had to work on it for one of my clients, I too found that AutoComplete Extender does not work. I followed the steps given in http://ajax.asp.net/ but I was left with the same impression that “AutoComplete Extender is not working” . Anyway try try again till you get gain is the formula I generally adopt. After googling of hours together, I found the solution. Though the solution is given in many forums/blogs, yet you need to give more attention to it to catch the point. So in this blog I tried to highlight the small but the important lines that require more attention. AutoComplete Extender AutoComplete is an ASP.NET AJAX extender that can be attached to any TextBox control, and will associate that control with a popup panel to display words that begin with the prefix typed into the textbox. Following are the steps:- Step 1 : Add autocomplete extender where you want to and configure it as explained in
  
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!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 runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <div>
    <asp:TextBox ID="txtSuggest" runat="server" ></asp:TextBox>
   <ajaxToolkit:AutoCompleteExtender runat="server"
ID="AutoCompleteExtender1"
 TargetControlID="txtSuggest"
 Enabled="true"
 MinimumPrefixLength="1"
     ServicePath="WebService.asmx"
ServiceMethod="GetSuggestions"

  />

    </div>
</form>
</body>
</html>
Step 2 : Add the following web service with name "WebService.asmx"and consume it :-
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;


///
/// Summary description for WebService
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class SuggestionService : System.Web.Services.WebService
{

    [WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public string[] GetSuggestions(string prefixText, int count)
    {
        int i = 0;
        DataView data = GetData();
        data = FilterData(data, prefixText);
        string[] suggestions = new string[data.Count];

        foreach (DataRowView row in data)
            suggestions[i++] = row["FirstName"].ToString();

        return suggestions;
    }

    private DataView GetData()
    {
        DataView view = (DataView)HttpContext.Current.Cache["Suggestions"];
        if (view == null)
        {
            SqlDataAdapter adapter = new SqlDataAdapter(
            "SELECT * FROM addressbook", System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"]
            .ConnectionString);
            DataTable table = new DataTable();
            adapter.Fill(table);
            view = table.DefaultView;

            // Store the entire data set to the ASP.NET Cache for
            // further reuse
            HttpContext.Current.Cache["Suggestions"] = view;
        }

        return view;
    }

    private DataView FilterData(DataView view, string prefix)
    {
        // Filter out undesired strings
        view.RowFilter = String.Format("FirstName LIKE '{0}%'", prefix);
        return view;
    }


}

No comments:

Post a Comment