5

I am looking for an example for using Typeahead.js with a Asp.Net Webmethod.

I have the example from http://twitter.github.io/typeahead.js/examples/ working with the basic example, but I don't fully understand how to implement the Bloodhound functionality with a asp.net webmethod.

The typeahead function is calling the WebMethod (I can see the step in the debugger) but there's nothing returned to the list for the TypeAhead.

Here's the markup:

<div class="input-group">
    <asp:TextBox ID="tboxText" runat="server" CssClass="form-control autocomplete" placeholder="Look Up"></asp:TextBox>
    <span class="input-group-btn">
        <asp:Button ID="btnAddItem" runat="server" Text="Add" CssClass="btn btn btn-amethyst" />
    </span>
</div>

And here's the jquery:

$(document).ready(function () {
        var textlookup = new Bloodhound({
            datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            remote: '/WebServices/InternalMethods.asmx/TextAutocomplete?param=%QUERY' 
        });

        textlookup.initialize();

        $('.autocomplete').typeahead({
            hint: true,
            highlight: true,
            minLength: 1
        },
        {
            name: 'textlookup',
            displayKey: 'value',
            source: textlookup.ttAdapter()
        });
    });

And here is the WebMethod:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<string> TextAutocomplete(string param)
    {
        return Suggestions.TextAutocomplete(param);
    }

Any help is appreciated.

Daniel Cheung
  • 4,779
  • 1
  • 30
  • 63
Bob Garick
  • 51
  • 3

1 Answers1

0

It looks as if you may need to get the parameter as a query string in c#. Try using return Suggestions.TextAutocomplete(Request.QueryString["param"]);

Mat Forsberg
  • 454
  • 2
  • 10