2011-12-07 12 views
0

我怎樣才能從客戶端調用代碼隱藏功能?來自jquery的函數調用代碼方

// Code-behind function: 
public void CodeBehindFunction(int i) 
{ 
    ... 
} 

我想從客戶端調用它...

// Client-side call: 
$(this). ?? 
+1

我建議您提供一些代碼並以其他方式展開您的問題,否則您會得到一些-1s來自您的方式... –

回答

0

正確你的意思是你要撥打的客戶端代碼服務器端程序...

我建議你強制使用JavaScript回發:__doPostback('CodeBehindFunction',iParameter); - 然後在你的服務器端代碼的頁面初始化事件中,通過類似的方式來捕獲此自定義回發。

if (Request.Params["__EVENTTARGET"] == "CodeBehindFunction") 
{ 
    int i; 
    // Try to convert the argument (in the example this is the value of iParameter) 
    if (int.TryParse(Request.Params("__EVENTARGUMENT"),out i) 
    { 
     CodeBehindFunction(i); 
    } 
} 

或者使用通過jQuery.ajax調用的WebService。

+1

我沒辦法回發,因爲這個頁面有很多回調函數 – hasanchuk

+0

那麼你應該使用一個WebService並用jQuery.ajax調用 –

+1

我正在使用客戶端回調,我無法通過客戶端回調來解決嗎? – hasanchuk

0

您可以使用Ajax調用CodeBehindFunction(int i)以 ,但你必須標記方法的WebMethod和靜態:

[WebMethod] 
public static void CodeBehindFunction(int i) 
{ 
    ... 
} 

然後從JS

$.ajax({ 
       type: "POST", 
       url: "/yourPage.aspx/CodeBehindFunction", 
       data: "{'i':'"+num+"'}", //PUT DATA HERE 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (msg) { 

       } 
      }) 

稱之爲** yourPage .aspx是聲明CodeBehindFunction方法的頁面