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);
    }
3
  • Why is GetArtists() a static method? Commented Jan 30, 2011 at 12:38
  • Got it - static is required to allow for ASP.NET AJAX callbacks to Web Methods in ASPX pages, read it here Commented Jan 30, 2011 at 13:16
  • There's a similar question on SO: stackoverflow.com/questions/2133194/… Commented Jan 30, 2011 at 13:30

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Thanks Marijn, I tried you solution and it works perfect :) However I ended up implementing my requirement as a web service.
0

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

ObjectFactory.GetInstance<IAristManager>.GetAll();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.