开发者

Put autocomplete methods in separate web file?

开发者 https://www.devze.com 2023-01-31 03:53 出处:网络
I have a web site with a growing number of AJAX calls. I have AJAX code that looks like this: function setupNameAutocomplete(id) {

I have a web site with a growing number of AJAX calls.

I have AJAX code that looks like this:

function setupNameAutocomplete(id) {
    $(id).autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "selectName.aspx/getNameAutocomplete",
    ....

Originally, the above javascript code was used on a web page where a user typed in the name of a person to search for; I decided I wanted to have autocomplete on the name.

However, as I continued to develop the web site, I decided that I wanted to be able to use the same "name" autocomplete on many pages. Logically, it made sense to put my javascript code in an external .js file instead of repeating it on every page.

It also makes sense that I would want to put the .net code that handles the AJAX in its own file as well. The .net autocomplete code looks like this:

   [WebMethod]
    public static IEnumerable<string> getNameAutocomplete(string text)
    {
        IEnumerable<string> values = lookupNamesThatStartWith(text);
        return values;
    }

Naturally, it seemed like this codes belongs in an external .asmx or perhaps a .ashx file, but I can't get my javascript code working unless I call the above code from selectName.aspx.

How can I get my AJAX .net code in a separate cod开发者_如何学Ce file?


If I understand your problem correctly, a viable solution would be to have a base class that your services inherit from that would implement the generic WebMethod.

That, or you could get it "pretty clean" by having the logic of the autocomplete lookup abstracted into a class and just call it from your WebMethods each time, as needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消