开发者

How to get the result of jQuery PageMethods function

开发者 https://www.devze.com 2022-12-27 23:22 出处:网络
I have posted a similar question to this problem and could answer it myself, but the solution does not fully satisfy me.

I have posted a similar question to this problem and could answer it myself, but the solution does not fully satisfy me.

Using Asp.Net/C#, I have a button which will perform some server-side code if it passes a client-side validation. I want to do it this way because for my application, I think it suits best.

<asp:Button runat="server" ID="addBtn" Text="Add" OnClientClick="if(validateEntry()== false){return false;}" OnClick="addBtn_Click"/>

I call a jQuery function named validateEntry() which uses PageMethods to get some data from the server. The data are necessary for the validation process.

function validateEntry() {

开发者_如何学CPageMethods.CheckEntry(params, function(result){//Evaluate result of PageMethods}
}

My problem is I do not know how to handle the data received from PageMethods properly. I am only able to hand them over to a sub-function which can use them. But I need the data in sort of variable to perform a return false.


I have posted a running example how to consume page methods from jquery here.


I worked out a solution, but would be thankful for easier ones.

I use a hidden field on the page and in the sub-function, I write the result from the page method into the field:

$("#pageMethodsResultField").val(result);

Afterwards, I can read the result from the field to use it in the main function.


Given that PageMethods.CheckEntry runs the passed function without delay you can do this:

function validateEntry() {
  var foo;
  PageMethods.CheckEntry(params, function(result){
    foo = "bar";
  });
  alert(foo);
}

If you try this and "foo" is not displayed, then the function is delayed and you need to rethink how to do this or provide more information so that we can help you.

0

精彩评论

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