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

  • 本文关键字:调用 静态 函数 aspx 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函数提前感谢

不可能直接通过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
        }
    }
    

尝试这个

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 中的调用函数

最新更新