views:

1036

answers:

4

Hi there, Im programming with Visual Studio 2008 and making a web application using .NET Framework 3.5 with C#. All DAL linked with an powerfull entity framework wrapper (similar to the one that VS.net 2010 would use.) I'm having a textbox used for a search on first and lastname. That problem i'm having, it's that i'm using AJAX Control Toolkit 2.0 which provides an Auto complete externer but, by using a WebServices (asmx). Is there any other way to use the auto complete without using a webservice?

Regards,

Simon

P.s.: Sorry for my english, i do my best :)!

+4  A: 

Yes, you can mark a method in a codebehind file as a webmethod:

   public partial class Products : System.Web.UI.Page 
   { 
     [System.Web.Services.WebMethod()] 
      [System.Web.Script.Services.ScriptMethod()] 
      public static string[] GetTerms(string prefix) 
      {
        // Put your logic here
      }
   }

See ASP.NET AJAX callbacks to Web Methods in ASPX pages.

Also, if you use NET 3.5 you can use the later Toolkit which incorporates the ComboBox, which is essentially a databound auto-complete control.

Dan Diplo
+6  A: 

You don't need to implement a separate web-service in order to provide a textbox with auto-complete functionality, however, you do need to provide the autocomplete extender with a valid web method that it can use to call upon to retrieve the list of matching entries.

The key properties for the AutoComplete functionality of the AutoComplete Extender control are the ServiceMethod and ServicePath properties. The ServiceMethod specifies the name of the web method that is called by the AJAX framework to retrieve the matching items for your autocomplete dropdown, and the ServicePath property specifies the full path and filename for the file that will contain the ServiceMethod properties' method. Note that the ServicePath property is, however, optional.

If you omit the ServicePath property, the AJAX framework will attempt to find the ServiceMethod web method in the code within your actual web page upon which the textbox and autocomplete extender are sited. This is usually in a "code-behind" file.

From the AutoComplete Sample page:

  • ServiceMethod - The web service method to be called. The signature of this method must match the following:

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

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.

  • ServicePath - The path to the web service that the extender will pull the word\sentence completions from. If this is not provided, the service method should be a page method.
CraigTP
Ahhh great!! Thanks a lot, it helps me out a lot. I'm trying it ... heum, RIGHT NOW :)!
Simon
+1  A: 

If you don't mind to use some JavaScript and make your hands a little bit dirty, then you can use auto-complete plug-in for jQuery or Prototype and then you can use a page for returning data or Webservice. Seejqery AutoComplete demo

Mostafa
A: 

Ok ... maybe i just don't understand how this control working. Here's my code :

Default.aspx

    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:TextBox ID="TextBox1" runat="server" />
    <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" 
        ServiceMethod="GetCompletionList" ServicePath="Codes/Utility.cs" 
        TargetControlID="TextBox1" UseContextKey="True" CompletionInterval="100" 
        MinimumPrefixLength="1">
    </cc1:AutoCompleteExtender>

~/Codes/Utility.cs

    public class Utility {

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public string[] GetCompletionList(string prefixText, int count) {
        return new string[] { "Bruno", "Simon", "Joanie", "Noémie", "Johanne", "Serge", "France", "Jacques", "Mylène" };
    }

}

But when i'm typing in the textbox, nothing is showing... Hum. Not sure what to do.

Simon
By putting a break point in my methods, i see that it just never go in ...
Simon
Remember to add the EnablePageMethods="true" property to your scriptmanager. Also, you should remove the ServicePath property of the AutoCompleteExtender if you are using a page method (see docs at http://ajaxcontroltoolkit.com/AutoComplete/AutoComplete.aspx )
Dan Diplo
Oh, sorry, it go through! But, no "Autocomplete" shown on my page.
Simon
Ohh well nice! it's workin! That Dan Diplo, i've forget to put EnablePageMethods.
Simon