2013-02-26 29 views
5

我想使用asp.net用戶控件AJAX,如何在asp.net用戶控件中使用jQuery ajax?

$.ajax({ 
     type: "POST", 
     url: "*TCSection.ascx/InsertTCSection", 
     data: "{id:'" + id + "'}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (msg) { 
      var URL = msg.d; 
      alert(URL); 
     }); 

的.cs代碼

[WebMethod] 
public static string InsertTCSection(string id) 
{ 
    string result = "deneme"; 
    return result; 
} 
+0

你得到一些錯誤? – 2013-02-26 11:42:21

+0

我加入的服務器端功能斷點,它是打不 – altandogan 2013-02-26 11:46:01

+0

你不能調用一個方法裏面保存的用戶控件通過jQuery的,因此在運行時有作爲的.ascx沒有這樣的資源,它合併到它保持 – 2013-02-26 11:52:17

回答

1

嘗試:

$.ajax({ 
     type: "POST", 
     url: "*TCSection.ascx/InsertTCSection", 
     data: JSON2.stringify({ id: id}, //Your 2nd id is passing value but i dont know where its come from 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (msg) { 
      var URL = msg.d; 
      alert(URL); 
      } 
     )}; 

和CS:

[WebMethod] 
public static string InsertTCSection(string id) 
{ 
    string result="deneme"; 
    return result; 
} 
+1

頁面ID參數不重要我想了解與.ascx有關的AJAX。有沒有辦法做到這一點? – altandogan 2013-02-26 11:50:55

1

我想你可能是米伊辛這個屬性

[System.Web.Script.Services.ScriptService] 

您服務上課前爲

[System.Web.Script.Services.ScriptService] 
public class TCSection: System.Web.Services.WebService 
{ 

    [WebMethod] 
    public static string InsertTCSection(string id) 
    { 
    } 
} 

或者可能有一個其他原因的web服務的這條道路是不正確的。

5

你不能呼叫的方法保持一個用戶控件內通過jQuery的。 ,因爲.ascx控件不代表真實的URL。

這意味着它們是被嵌入在一些ASP.NET頁面,因此它們不運行時存在。 你可能做的是,創建一個單獨的服務,把你方法那裏。 像webservices.

看到thisthis和許多其他

2

我使用通用處理器來解決這個問題。

相關問題