0

i want to call a c# function from aspx page i tried it like below

 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) {

             }
         });

     }

my problem is its works fine when i write the c# function as static , but other wise it will not work, i want to know is there any method for calling a non-static c# function from aspx page Thanks in advance

Arunprasanth K V
  • 20,733
  • 8
  • 41
  • 71
  • 2
    Possible duplicate: http://stackoverflow.com/questions/1360253/call-non-static-method-in-server-sideaspx-cs-from-client-side-use-javascript?lq=1 (and may others) – Olaf Nov 04 '14 at 06:49
  • 1
    Thanks for your quick replay .but it will not work in my program – Arunprasanth K V Nov 04 '14 at 07:30

2 Answers2

1

There are no possibility to run function from aspx page directly via url.

Try the following:

  1. Change your ajax request as below:

    $.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. Update Page_Load handler in your page:

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

try this

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

instead of

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

and call function in pageload

Code9090
  • 21
  • 5