0

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

ShalevBs
  • 46
  • 3

1 Answers1

1

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

}
Community
  • 1
  • 1
Samuel
  • 1,149
  • 8
  • 25
  • What you mean by Json format?, What I'm sending over there? the parameters for the ASP.Net Function? – ShalevBs Apr 30 '14 at 15:28
  • I change the code to use the parameters like json. The function that you will call em seerver side need to have [WebMethod] annotation, and param.name, the property name os param need to be the same name os the method parameter. – Samuel Apr 30 '14 at 16:39
  • I've Changed it as you said, and changed also the directory position (url: '<%= ResolveUrl("~../default.aspx/updateit") %>',) added error: alert("Error") and it keep alerting me that I got an error – ShalevBs Apr 30 '14 at 16:49
  • try use default.aspx/UpdateIT, can you send the error message? – Samuel Apr 30 '14 at 17:02
  • Does UpdateIT have with annotation [WebMethod]? – Samuel Apr 30 '14 at 17:14
  • Use "public static void UpdateIT" A webmethod need to be static, because it need to exist every time and not only when a class was instantiated – Samuel May 01 '14 at 12:42