views:

67

answers:

3

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() {

PageMethods.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.

A: 

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

korchev
Thank you, but my page method is running too. I know the question is stupid, but I don´t know how to handle the result given by the page method. Basically, I want to use the result to make the method validateEntry() return false.If the answer lies in your posted example, please forgive and explain, I don´t get it...
Jan-Frederik Carl
A: 

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.

Jan-Frederik Carl
A: 

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.

svinto