0

I have a jquery-ui autocomplete textbox working fine on my asp.net webforms site.

The problem with it running this way, is that asp.net is generating a huge array which has to be loaded before the page can be displayed.

My solution is to provide the data in a webservice, and point my autocomplete box to the url of the webservice, so that I can look up my entries on-the-fly, AFTER the page is loaded.

I know this will work, but I'm not really familiar with all that asp.net offers, and so my question is more one of "what is the best way to go about this?"

Since the data belongs to the application itself, and since I don't need to access the data from any outside the application, I'm wondering if making a webservice is the correct way to do this? For instance, does asp.net have something like a webservice that is only accessible within the same application?

Brent
  • 16,259
  • 12
  • 42
  • 42

2 Answers2

1

I recommend to use WCF service, which can be configured to be used in JavaScript and to be called only from the same domain. See http://www.aspnetajaxtutorials.com/2010/11/jquery-autocomplete-with-aspnet-wcf.html

Ivan Doroshenko
  • 944
  • 7
  • 13
1

If you're going to do a lot of web services, then go with WCF as stated in the other answer. For longer explanation of WCF vs ASMX, see What is the difference between WCF and ASMX web services?.

But if you just need a one off thing, try doing an ASMX Web Service which is simpler and has less overhead. See HOW TO: Write a Simple Web Service by Using Visual C# .NET by Microsoft.

Put this in AutoCompleteServices.asmx.cs:

[WebService]
public class AutoCompleteServices
{
    [WebMethod]
    public void GetAutoCompleteResponses(string enteredText)
    {
        List<string> possibleResponses=GetPossibleResponses(enteredText);
        string json=JsonConvert.SerializeObject(possibleResponses);
        HttpContext.Current.Response.ContentType="application/json";
        HttpContext.Current.Response.Write(json);
    }
}

Create an AutoCompleteServices.asmx file:

<%@ WebService Language="c#" Codebehind="AutoCompleteServices.asmx.cs" Class="AutoCompleteServices" %>

Notice that unlike some WebMethod's you may have seen, I use a void return type and I write directly to the Response. This is due to poor Json support by .NET out of the box and allows you to customize your Json string (for example, maybe you want the Json string properly indented for debugging purposes etc).

Community
  • 1
  • 1
mason
  • 31,774
  • 10
  • 77
  • 121