2014-11-04 29 views
0

我想打電話從aspx頁面C#函數的調用我試圖像下面如何使ASPX頁面上的非靜態C#功能

function DeleteKartItems(callback) { 

    $.ajax({ 
      type: "POST", 
      url: 'About.aspx/updatingdatabase',// my function name in c# 
      data: '{"username":"' + col1 + '","password":"' + col2 + '","age":"' + col3 + '","city":"' + col4 + '","id":"' + idlast + '"}', 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (data) { 
           var x = data.d;// i am trying to store the return data into a local variable 

      }, 
      error: function (e) { 

      } 
     }); 

    } 

我的問題是,它的作品很好,當我寫的C#函數爲靜態,但其他明智不會 工作,我想知道是否有任何方法調用從靜態c#函數從aspx頁 在此先感謝

+2

可能重複:http://stackoverflow.com/questions/1360253/call-non-static-method-in-server -sideaspx-cs-from-client-side-use-javascript?lq = 1(可能是其他人) – Olaf 2014-11-04 06:49:18

+1

感謝您的快速回放。但它不會在我的程序 – 2014-11-04 07:30:31

回答

1

有沒有可能從aspx頁運行功能直接通過url。

嘗試以下操作:

  1. 更改您的Ajax請求如下:

    $.ajax({ 
        type: "POST", 
        url: 'About.aspx', 
        data: '{"method":"updatingdatabase","username":"' + col1 + '","password":"' + col2 + '","age":"' + col3 + '","city":"' + col4 + '","id":"' + idlast + '"}', 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        success: function (data) { 
             var x = data.d;// i am trying to store the return data into a local variable 
    
        }, 
        error: function (e) { 
    
        } 
    }); 
    
  2. 更新Page_Load處理程序在你的頁面:

    protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!String.IsNullOrEmpty(Request["method"]) && String.Compare(Request["method"], "updatingdatabase", true) == 0) 
        { 
         UpdatingDatabase(); //run the method 
        } 
    } 
    
+0

中感謝您的重播 – 2014-11-04 11:32:54

0

試試這個

data: '{"method":"updatingdatabase","username":"' + col1 + '","password":"' + col2 + '","age":"' + col3 + '","city":"' + col4 + '","id":"' + idlast + '"}', 

代替

url: 'About.aspx', 
    data: '{"method":"updatingdatabase","username":"' + col1 + '","password":"' + col2 + '","age":"' + col3 + '","city":"' + col4 + '","id":"' + idlast + '"}', 

,並調用函數頁面加載