如何在aspx页面上调用非静态C#函数

时间:2014-11-04 06:42:20

标签: asp.net

我想从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#函数写为静态时它的工作正常,但其他方面它不会 工作,我想知道是否有任何方法从aspx页面调用非静态c#函数 提前致谢

2 个答案:

答案 0 :(得分:1)

不可能直接通过url从aspx页面运行函数。

尝试以下方法:

  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
        }
    }
    

答案 1 :(得分: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 + '"}',

并在pageload中调用函数