0

I am trying to make a jquery ajax call to a static method on the codebehind file. The problem is that ArtistManager injected by Spring is not static and I cannot use it in the static webmethod. I am looking for any ideas on how to implement this

ArtistList.aspx

$(document).ready(function () {
        $("#Result").click(function () {
            $.ajax({
                type: "POST",
                url: "ArtistList.aspx/GetArtists",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    $("#Result").text(msg.d);
                    alert("Success: " + msg.d);
                },
                error: function (msg) {
                    // Replace the div's content with the page method's return.
                    $("#Result").text(msg.d);
                    alert("Error: " + msg.d);
                }
            });
        });
    });

ArtistList.aspx.cs

    private IArtistManager artistManager;
    public IArtistManager ArtistManager
    {
        set { this.artistManager = value; }
        get { return artistManager; }
    }
    protected long rowCount = 0;
.
.
.

    [WebMethod]
    public static IList<App.Data.BusinessObjects.Artist> GetArtists()
    {
        //return ArtistManager.GetAll("Name", "ASC", 1, 100, out rowCount);
    }
Guray
  • 5
  • 2
  • Why is GetArtists() a static method? – Marijn Jan 30 '11 at 12:38
  • Got it - static is required to allow for ASP.NET AJAX callbacks to Web Methods in ASPX pages, read it [here](http://geekswithblogs.net/frankw/archive/2008/03/13/asp.net-ajax-callbacks-to-web-methods-in-aspx-pages.aspx) – Marijn Jan 30 '11 at 13:16
  • There's a similar question on SO: http://stackoverflow.com/questions/2133194/access-asp-net-control-from-static-webmethod-js-ajax-call – Marijn Jan 30 '11 at 13:30

2 Answers2

1

Assuming a single context, in which an IArtistManager is configured named artistManager:

using Spring.Context.Support;
// ...
[WebMethod]
public static IList<App.Data.BusinessObjects.Artist> GetArtists()
{
  IApplicationContext context = ContextRegistry.GetContext(); // get the application context
  IArtistManager mngr = (IArtistManager)context.GetObject("artistManager"); // get the object
  return mngr.GetAll("Name", "ASC", 1, 100, out rowCount);
}

Note that this code won't compile either, since rowCount is an instance member, similar to artistManager in your question.

Marijn
  • 10,367
  • 5
  • 59
  • 80
  • The approach of Marijn is correct, pass the rowcount in as a parameter to the getartists fucntion. Simularly pass the rowcount and list of artists both to the client and so on. – BennyM Feb 01 '11 at 10:14
  • Thanks Marijn, I tried you solution and it works perfect :) However I ended up implementing my requirement as a web service. – Guray Feb 10 '11 at 18:01
0

Don't know about spring, but I am sure it has something like structure map.

ObjectFactory.GetInstance<IAristManager>.GetAll();
epitka
  • 17,275
  • 20
  • 88
  • 141