开发者

Call code behind method from Jquery

开发者 https://www.devze.com 2022-12-23 23:06 出处:网络
I have this question: How could I call a codebehind method from jquery? I have a web page: (ForgetPass.aspx) with its respective codebehind (ForgetPass.aspx.cs). On the codebehind I have a public met

I have this question:

How could I call a codebehind method from jquery? I have a web page: (ForgetPass.aspx) with its respective codebehind (ForgetPass.aspx.cs). On the codebehind I have a public method:

public void ChangeSession(string strChangeSession)
    { some stuff... }

When I was working on MVC, calling that method was as simple as:

$.post("MyPageController/ChangeSession", { strChangeSession: 'yes' });

But now that I开发者_运维技巧'm on aspx/C# I don't know how to call that method from jquery (or maybe I need to use [WebMethod] clause on the codebehind ??)

Thanks in advance.


There's a full run down here that explains the whole process :)

Yes you'll need [WebMethod] and it'll need to be static as well, read the tutorial for an explanation on these bits.

Based on your method names, you'll probably need this:

[WebMethod (EnableSession = true)] //Allows access to session state
public void ChangeSession(string strChangeSession)
{ some stuff... }


You need to make the page method as static and also need to mark it as [WebMethod] so that you can access it using jquery ajax like:

var loc = window.location.href;
$.ajax({
     type: 'POST',
      url: loc + "/GetMessage",
      data: "{}",
      contentType: "application/json; charset=utf-8"

    })
    .success(function (response) {
      alert(response.d);

    })
    .error(function (response) {
      alert(response.d);
    });

Get complete sample here: http://www.codegateway.com/2012/05/jquery-call-page-codebehind-method.html


Several ways to accomplish this. Like above, look at the .NET PageMethod/WebMethods. It will show you how to build the WebMethods and call them from Javascript.

I've been switching away from .NET pre-built stuff and going with straight jQuery ajax calls. It's the same thing, but using jQuery you have a little more control over the call and what it returns.

jQuery.ajax({
    type: "POST",
    url: "edit.aspx/yourmethodname",
    data: "{yourmethodparam:" + somevar + "}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(response) {
        alert(response.d);
    }
error: function(err, response) {
    alert("error");
}
});

It's more complex, but you get more control over what type of object is returned, JSON or text.

Remember, response.d the "d" is the JSON object if you decide to return more then just a simple type, like string or integer. You can return objects like lists and such. These will be converted to JSON objects. If it were a simple type like an integer, then just 'response' would be the value.

If you decide to go more complex and use JSON return objects, watch the 'datatype' property, sometimes I found .NET returning string objects that had to be converted into JSON using, json2.js file - http://www.json.org/js.html. It threw me for a loop for a long time. It's a good site that will explain JSON, as that's what the PageMethod/WebMethods are really using.

0

精彩评论

暂无评论...
验证码 换一张
取 消