2011-01-28 38 views
0

我想對代碼隱藏文件上的靜態方法進行jquery ajax調用。問題是由Spring注入的ArtistManager不是靜態的,我不能在靜態web方法中使用它。我要尋找關於如何實現這一spring.net + nhibernate + jquery ajax在代碼隱藏中調用webmethod

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); 
    } 
+0

GetArtists()爲什麼是靜態方法? – Marijn 2011-01-30 12:38:16

+0

明白了 - 靜態是允許ASPX頁面中的ASP.NET AJAX回調Web方法,閱讀它[這裏](http://geekswithblogs.net/frankw/archive/2008/03/13/asp.net- ajax-callbacks-web-methods-in-aspx-pages.aspx) – Marijn 2011-01-30 13:16:33

回答

1

假設一個上下文,其中IArtistManager被配置命名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); 
} 

注意,該代碼將無法編譯,因爲相關rowCount是一個實例成員,類似於artistManager你題。

0

不知道春天什麼想法,但我我確定它有類似結構圖的東西。

ObjectFactory.GetInstance<IAristManager>.GetAll();