2

I followed a very simple example of autocompletion using ajax and cannot get mine to work. I stepped threw the code and it looks like it never goes to my webmethod. What else is there to check?

.aspx

    <asp:ScriptManager ID="ScriptManager1" runat="server">

    </asp:ScriptManager>

<asp:TextBox ID="txtFrom" runat="server">
</asp:TextBox>
<ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="txtFrom" MinimumPrefixLength="1" ServiceMethod="GetSuggestions">
</ajaxToolkit:AutoCompleteExtender>

.aspx.cs

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string[] GetSuggestions(string prefixText, int count, string contextKey)
{
    string[] members = { "Nick", "John", "Bob" };

    return (from m in members where m.StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase) select m).Take(count).ToArray();
}

I created a webservice to use instead of putting my method directly in the class, but I get the same result.

1
  • Is your web method static? It can only be static if it's within a page. Commented Apr 22, 2011 at 19:28

2 Answers 2

2
public static string[] GetSuggestions(string prefixText, int count, string contxtKey)

You wrote contxtKey instead of contextKey

Remember, the signature (type, parameters name, return type) has to match exactly.

If the context key is used, it should have the same signature with an additional parameter named contextKey of type string:

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public string[] GetCompletionList(
    string prefixText, int count, string contextKey) { ... }

Note that you can replace "GetCompletionList" with a name of your choice, but the return type and parameter name and type must exactly match, including case.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, I changed it to contextKey, but it still doesn't call it/work. Shouldn't it at least jump to the method during stepping through it?
Ok, I just removed the whole string contextKey and it now works. Thanks
You're welcome, the function including "string contextKey" should work if you add both "UseContextKey" and "ContextKey" attributes to the AutoCompleteExtender control.
1

It does not appear that your scriptmanager is configured to allow for page methods, but that your "GetSuggestions" is a page method (on an .aspx.cs) rather than a service call (on an .asmx).

Try editing the script manager to look like:

<asp:ScriptManager
ID=”scriptManager”
EnablePageMethods=”true”
runat=”server” >

If you want to move your method call to an .asmx then your AutoCompleteExtender should be modified to reference the .asmx file like this:

<ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="txtFrom" MinimumPrefixLength="1" ServiceMethod="GetSuggestions" ServicePath="AutoCompleteService.asmx">
                    </ajaxToolkit:AutoCompleteExtender>

I think you just tried to blend the two techniques and ended up just little bit off.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.