ShalevBs
ShalevBs

Reputation: 46

Call ASP.Net Function from Jquery

Tried to look after this, and Couldn't find a good explaination..

    function updateusers() {
    var columns = ["username", "password", "email", "adminlevel", "usertype", "gender", "regdate", "lastlog"]
        for (var row = 2; row <= $('#usertable').children().children().length; row++) {
            for (var col = 0; col < 8; col++) {
                if ($('[name=' + row + '_' + columns[col] + ']').val() != 0) {
                    ###ASP.Net Function###
                    UpdateIT($('[name=' + row + '_' + columns[col] + ']').val())
                    ###ASP.Net Function###
            }
        }
    }
}

I Understood I can do it with Ajax, But Couldn't understand properly How.. I Got a Function Called UpdateIT in my Default.aspx, and I want to call it

Upvotes: 0

Views: 785

Answers (1)

Samuel
Samuel

Reputation: 1149

Try this.

function updateusers() {
        var columns = ["username", "password", "email", "adminlevel", "usertype", "gender", "regdate", "lastlog"]
            for (var row = 2; row <= $('#usertable').children().children().length; row++) {
                for (var col = 0; col < 8; col++) {
                    if ($('[name=' + row + '_' + columns[col] + ']').val() != 0) {
                        ###ASP.Net Function###
                        var param = {};
                        param.name =  row + '_' + columns[col];
                        $.ajax({
                           type: 'POST',
                           url: '<%= ResolveUrl("~/default.aspx/UpdateIT") %>',
                           data: JSON.stringify(param),
                           contentType: 'application/json; charset=utf-8',
                           dataType: 'json',
                           success: function (msg) {
                           alert(msg.d)
                       }
                       });
                       ###ASP.Net Function###
                }
            }
        }
    }

see this link too

Calling a webmethod with jquery in asp.net webforms

EDIT------------------

I implement this test and it´s worked here. Try it.

cs

[WebMethod]
public static void UpdateIT(string name)
{
    throw new Exception("I´m here");
}

js

function tryCallUpdateIT() {

    var param = {};
    param.name = '1' + '_' + "value";
    $.ajax({
        type: 'POST',
        url: 'default.aspx/UpdateIT',
        data: JSON.stringify(param),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (msg) {
            alert(msg.d)
        }
    });

}

Upvotes: 1

Related Questions