galvin Verghese
galvin Verghese

Reputation: 605

Using ajax autocompleteextender control in asp.net

I am using ajax AutoCompleteExtender control and using .net 3.5. I add the dll successfully in my toolbox tab.
So first of all i create a website and then drag the script manager, textbox and AutoCompleteExtender control in my default.aspx page. Then i add webservice and define one web method i.e. GetSuggestions(). My program is executed successfully but i am not getting the correct output. When i put any text in textbox, it is not populating any suggestion. Please help me.

Here is my webservice code:

using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 

[System.Web.Script.Services.ScriptService]
[System.ComponentModel.ToolboxItem(false)]

public class MyAutocompleteService : WebService
{
    [WebMethod]

    public string[] GetSuggestions(string prefixText, int count)
    {
        List<string> responses = new List<string>();

        for (int i = 0; i < count; i++)

            responses.Add(prefixText + (char)(i + 65));

        return responses.ToArray();

    }

}                                                                                               

And Here is my default.aspx code:

<%@ 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 runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services >
        <asp:ServiceReference Path="~/MyAutocompleteService.asmx" />
        </Services>
        </asp:ScriptManager>
        <br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />

        <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" 
            ServiceMethod="GetSuggestions" ServicePath="MyAutocompleteService.asmx" 
            TargetControlID="TextBox1" MinimumPrefixLength ="1" 
            CompletionSetCount ="12" EnableCaching="true">
        </asp:AutoCompleteExtender>
        <br />
        <br />

    </div>
    </form>
</body>
</html>

So please help me where i am missing.

Upvotes: 0

Views: 1956

Answers (2)

user1916189
user1916189

Reputation: 21

In order to see the results from the web service, you must all add div that will be populated with the results. I'd edited some of these above code.

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    **<div id="resultsPanel"></div>**
    <br />

    <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" 
        ServiceMethod="GetSuggestions" ServicePath="MyAutocompleteService.asmx" 
        TargetControlID="TextBox1" MinimumPrefixLength ="1" 
        CompletionSetCount ="12" EnableCaching="true"
        **CompletionListElementID="listPlacement"**>
    </asp:AutoCompleteExtender>

I hope that helps.

Upvotes: 2

Saurabh
Saurabh

Reputation: 5727

Try to use path without "~" sign

<asp:ServiceReference Path="MyAutocompleteService.asmx" />

Upvotes: 0

Related Questions