John
John

Reputation: 297

WebMethod not being called. ASP.NET C#

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.

Upvotes: 2

Views: 5539

Answers (2)

Cos Callis
Cos Callis

Reputation: 5084

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.

Upvotes: 1

Larry
Larry

Reputation: 18031

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.

Upvotes: 2

Related Questions