2011-04-28 56 views
0

只是試圖看看我是否可以在使用它的用戶控件(通過客戶端)明確添加定義/註冊AJAX Web服務。 Web服務被定義在與使用它的控件相同的服務器上,但該控件無法使用它。註冊或使用Web服務在用戶控制,而不是母版頁

當我在主頁上添加對SriptManager上的Web服務的引用時有效,但我想專門添加它以供自定義控件使用,然後不想將其從主頁面中刪除。

方案: 我已經試過: <%@註冊的TagPrefix = 「ajaxws」 大會= 「的AssemblyName」 命名空間= 「WebService.AjaxNameSpace」 %>

讓我知道你需要一些更多的細節。我一直在研究這個問題的修復,但我發現的唯一可靠的修復方法是在母版頁上將.asmx添加到ScriptManager。

+0

JQuery是一個可用選項嗎? – 2011-04-28 20:24:10

回答

0

您似乎在嘗試向腳本管理器添加腳本引用或服務引用。

您可以在用戶控件的代碼隱藏做到這一點,使用的語句,如:

ScriptManager.GetCurrent(this.Page).Scripts.Add(/* Whatever it is you want to add */); 

...在您需要的Script Reference類,或...

ScriptManager.GetCurrent(this.Page).Services.Add(/* Whatever it is you want to add */); 

...對此您需要Service Reference類。

請注意,如果頁面沒有腳本管理器,則任何一種方法都可以適用。

+0

如果你想推薦腳本管理器路徑,我只需要說「確保母版頁有一個ScriptManager」,並在控件上放置一個ScriptManagerProxy。這很容易管理關係。 – 2011-04-28 20:38:07

+0

有沒有一種方法可以在.ascx中添加ScriptManagerProxy而不改變代碼隱藏(避免編譯修改)。母版頁總是有一個默認的ScriptManager。問題是隻有某些控件需要使用該Web服務,所以它不需要被其他頁面使用。我無法控制更改代碼隱藏功能。 – 2011-05-06 16:50:09

+0

@Amy,腳本管理器代理_應該被添加到.ascx中。但是這**會改變編譯。您將不得不重新編譯以獲取新的引用。腳本管理器和代理進行協調以確保在需要時添加引用,但只添加一次。 – 2011-05-06 18:12:58

0

如果你能夠在你的解決方案中使用jquery,並且你想從客戶端完全使用jquery。你可以這樣做:

<script type="text/javascript"> 
    function CallService() { 
     $.ajax({ 
      type: "POST", //You can use POST or GET (GET works a little different) 
      url: "YourWebService.asmx/GetSomeData", //This assumes a relative path, you can use any url. 
      data: "{}", 
      contentType: "application/json; charset=utf-8", // you must define the content type (though I am not sure what other options are of any value here...) 
      dataType: "json", //you can do json or XML serialization, do json, it is easier in the long run. 
      success: Success, //this is a reference to the "function Success" to be invoke by the the service return. 
      error: Error //ditto for the function Error... 
     }); 
    } 

    function Success(data, status) { 
     //do what you want with the data... 
    } 

    function Error(request, status, error) { 
     //something in the soup ain't right... 
    } 
</script> 

這實際上和使用ajax和jQuery調用Web服務一樣容易。

+0

非常感謝您的意見。不幸的是,JQuery不是首選(不是我):(:( – 2011-05-06 16:45:01