views:

1154

answers:

7

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"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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.

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

be sure to include the ScriptMethod attribute.

blesh
Tried that, same thing
Dave
A: 

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.

John Rasch
Tried that too - still just getting the Doctype and the rest of the sourcecode letter by letter in the results...I have ajax UpdatePanels working elsewhere on the site so I know that is ok, it's just this thing from the Toolkit
Dave
Try putting a breakpoint in the `WebMethod` and see if it gets hit
John Rasch
No, it's not hitting the breakpoint...
Dave
When I was trying a local webservice instead of PageMethods I couldn't get them to fire either, even testing the .asmx with the Invoke button just hung the browser - is it possible there is a bigger issue with permissions or something on this?
Dave
+2  A: 

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

Demetri
This is using PageMethods, so I thought you had to exclude the ServicePath, that's only for WebServices - tried it and still get the problem
Dave
+2  A: 

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>
Jason
This is using PageMthods, so I thought you had to exclude the ServicePath, that's only for WebServices - tried it and still get the problem
Dave
I use page methods as well, and needed the service path. Did you have the service path, the method defined as static, and the ScriptMethod attribute defined all at the same time? That autocomplete control is pretty particular.
Jason
A: 

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.

Dave
I've narrowed it down - its the Context.Rewrite in Global.asax that is causing the problem. Question now is how I get around that...
Dave
I've added in some code to Global.asax which only rewrites the url if it has a file extension other than .axd. So if it has .aspx or something else according to the re-write rules it gets checked and re-written, if not it gets left alone.Thanks to everyone for the help anyway...
Dave
A: 

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.

Roan
A: 

Hi Dave,

I am also getting a similar issue. When i use page static method i get the html of the whole page and IE displays a list of Undefined. Please let me know if you get the solution. I need it very badly :( :( :(

Taher