Arunprasanth K V
Arunprasanth K V

Reputation: 21951

How to make call of a Non-Static C# function on aspx page

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

Upvotes: 0

Views: 187

Answers (2)

Code9090
Code9090

Reputation: 21

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

Upvotes: 0

Pavel Timoshenko
Pavel Timoshenko

Reputation: 721

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

Upvotes: 1

Related Questions