0

(Before marking this question as duplicate, I've tried all the other questions, most of them have outdated links and doesn't solve my problem)

I'm trying to make a simple autocomplete function but the Code-Behind is never called.

Login.aspx:

<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
    <asp:TextBox ID="TextBox1" runat="server" autofocus="autofocus"></asp:TextBox>
    <cc1:AutoCompleteExtender ID="ACE" runat="server" ServiceMethod="GetCompletionList" 
                              ServicePath="~/App_Code/Common.cs" 
                              TargetControlID="TextBox1" 
                              MinimumPrefixLength="1" 
                              CompletionSetCount="10" >
    </cc1:AutoCompleteExtender>
</form>

Common.cs:

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string[] GetCompletionList(string prefixText, int count)
{
    return new string[] { "test1", "test2", "test3" }
}
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
Lucas
  • 534
  • 1
  • 10
  • 29
  • 1
    `ServicePath` shouldn't be a .cs file, should it? I would think it would be looking for a `.asmx` or `.svc` – ragerory Jul 24 '15 at 13:07
  • I don't have neither. Should I try removing the ServicePath? (tried, didn't work) – Lucas Jul 24 '15 at 13:08
  • When you debug this, how specifically does it fail? Is the client-side code calling the server at all? What is the URL it's calling? What is the server's response? You need to do at least *some* debugging. – David Jul 24 '15 at 13:09
  • I did, David, as you can see in the title, the function is not being called at all. – Lucas Jul 24 '15 at 13:10
  • 1
    @Lucas no, if you remove `ServicePath` you will definitely not hit anything. You can take the logic that's in that class, and create a separate `WCF` project and implement the methods in there, then call that `.svc` from your `.aspx` – ragerory Jul 24 '15 at 13:12
  • 1
    @David all that information is in the OP's question. 1. No function being called. 2. The URL it's calling is in the `ServicePath` + `ServiceMethod`, 3. No response since it's not hitting a method. You need to do at least *some* reading. – ragerory Jul 24 '15 at 13:14
  • @ragerory: "No function being called" in the server-side code isn't the same as no AJAX request being made to the server. The request could very well be made and the server responding with a useful error message that's simply being ignored. You need to have at least *some* understanding of how web development works in order to be helpful. – David Jul 24 '15 at 13:15
  • @Lucas: Just because the framework isn't reaching the function doesn't mean no AJAX request is being made from client-side code. Check your browser's debugging tools, there could be useful error information in there. JavaScript errors, AJAX requests/responses, etc. – David Jul 24 '15 at 13:17
  • 1
    @David my web dev knowledge is just fine. There is enough information here to be able to help the OP is what I'm saying. It's not like he just asked "AutoExtender doesn't work". I felt he gave sufficient information to reproduce his problem. – ragerory Jul 24 '15 at 13:23
  • 1
    @Lucas If you remove `ServicePath` from your control, and move `GetCompletionList` method to `Login.aspx.cs`, does it work? I was able to get it working like that. – ragerory Jul 24 '15 at 13:25
  • @ragerory removing ServicePath and putting GetCompletionList in Login.aspx.cs solved my problem! Thank you very much! – Lucas Jul 24 '15 at 14:02

1 Answers1

1

Unfortunately, you cannot use an ASP.NET AJAX page method within a .cs class file unless it derives from the Page class or derives from another class that derives from the Page class (the Page class must be in the inheritance hierarchy). This is the reason that you cannot use ASP.NET AJAX page methods in ASP.NET master pages, because they inherit from the MasterPage class, which is not part of the Page class inheritance hierarchy.

You have, at least, 2 options:

1) Put the GetCompletionList method in your code-behind file Login.aspx.cs and then you can omit the ServicePath property from your auto-complete extender markup.

2) Create a Common.aspx page that will hold ASP.NET AJAX page methods that can be used across pages in your application. Since the only thing in this .aspx file will be static page methods nothing would be rendered if the user navigated to that page, but it does cause confusion for someone that does not know what ASP.NET AJAX page methods are and thinks they should delete a blank page. It could also be confusing for your users if they somehow type in that URL within the address bar of your application.

Now you can have the auto-complete extender's ServicePath property point to the page method in Common.aspx, like this:

ServicePath="Common.aspx"

Note: You can call across .aspx pages for ASP.NET AJAX page methods, which is what allows this Common.aspx approach to be usable.

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80