Tearing my hair out - ASP.Net AJAX AutoComplete no

2019-05-22 11:47发布

问题:

Hope someone can help with this. I've been up and down the web and through this site looking for an answer, but still can't get the Autocomplete AJAX control to work. I've gone from trying to include it in an existing site to stripping it right back to a very basic form and it's still not functioning. I'm having a little more luck using Page Methods rather than a local webservice, so here is my code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="droptest.aspx.cs" Inherits="droptest" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!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></title>
</head>
<body>  
    <form id="form1" runat="server">    
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server">
        </asp:ScriptManager>
        <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" 
            MinimumPrefixLength="1" ServiceMethod="getResults" 
            TargetControlID="TextBox1">
        </cc1:AutoCompleteExtender>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Services;
using System.Web.Services;

public partial class droptest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public string[] getResults(string prefixText, int count)
    {
        string[] test = new string[5] { "One", "Two", "Three", "Four", "Five" };
        return test;
    }
}

Tried to keep things as simple as possible, but all I get is either the autocomplete dropdown with the source of the page (starting with the <! doctype...) letter by letter, or in IE7 it just says "UNDEFINED" all the way down the list.

I'm using Visual Web Developer 2008 at the moment, this is running on Localhost. I think I've exhausted all the "Try this..." options I can find, everything from adding in [ScriptMethod] to changing things in Web.Config.

Is there anything obviously wrong with this code?

Only other thing that may be having an effect is in Global.asax I do a Context.RewritePath to rewrite URLs - does this have any effect on AJAX?

Thanks for any help you can give.

回答1:

You also need to include your page name as the servicePath, I think.

    <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" 
            MinimumPrefixLength="1" ServiceMethod="getResults" ServicePath="droptest.aspx" 
            TargetControlID="TextBox1">
    </cc1:AutoCompleteExtender>


回答2:

Try adding ServicePath to the cc1:AutoCompleteExtender with the path to the web service.



回答3:

[WebMethod, ScriptMethod]
public string[] getResults(string prefixText, int count)
{

be sure to include the ScriptMethod attribute.



回答4:

Right, something I've added in from these suggestions has worked!!! Still have a problem though, it works in a standalone project but adding it back into the existing project and it's not working again. So thanks for the help so far, I have a working example, just have to figure out what is killing it inside the other project now.



回答5:

Make the method static:

[WebMethod]
public static string[] getResults(string prefixText, int count)
{
        string[] test = new string[5] { "One", "Two", "Three", "Four", "Five" };
        return test;
}

Update:

A shot in the dark... try moving the ScriptManager above the textbox. Also, I would set the ServicePath to "~/" simply because you mention the URL rewrites.



回答6:

If you are using IIS 5.1, try to temporarily remove .* from the application setting. This wildcard setting prevents AJAX like control to work correctly.



回答7:

In my case, my project use rewrite rule to remove the extension aspx. I thing the problem is that. I comment the rewrite rule in web.config. Then clear solution. Rebuild it. Clear all history in firefox / chrome (what you use). Then Ctrl+F5 or F5. Autocomplete show correctly.