?
Solved

Ajax, how wait for the response of the PageMethods call result ?

Posted on 2008-10-30
3
Medium Priority
?
1,602 Views
Last Modified: 2013-12-17
Hi, make a call to a function in my code behind (C#) from javascript with Ajax

My function 'Test' call my Function 'Toto' and then after i have no control,  my 'Test' function continue running,  so my var MyResult is always null in my 'Test' method.

I would like to wait for the result of OnSucceedd to continue running my 'Test' function, so after the call to 'Toto' i have my var 'MyResult' set by the result of my OnSucceedd.


Here is my code  :


var MyResult  = null;

function Test()
{
      Toto();

      if(MyResult == true)
            blablabla...
}

Toto()
{
     PageMethods.FindRequesterNameMatch(1, 2, onSucceeded, onFailed);
}


function onSucceeded(result, userContext, methodName)
{
        MatchExist = result;
}

function onFailed(error, userContext, methodName)
{
        MatchExist = false;
}
0
Comment
Question by:dany651
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 22847158
>>my 'Test' function continue running,  so my var MyResult is always null in my 'Test' method
Correct, but once Toto has completed, it should call onSucceeded. So the if should really be in onSucceeded (although it's not clear where you are setting the value of MyResult):
function onSucceeded(result, userContext, methodName)
{
        MatchExist = result;
 
if(MyResult == true)
            blablabla...
 
}

Open in new window

0
 

Author Comment

by:dany651
ID: 22870792
I know but my Test function are called by a button ( OnClick="return Test();"  ) so i want to verify the result before continue.....

TEST ==

result = called my webMethod via Ajax....

IF  MyResult == true
           return true to button and execute ButtonClick() server side code
ELSE
            return false to button and stop process              
0
 
LVL 82

Accepted Solution

by:
hielo earned 1000 total points
ID: 22947278
>>my Test function are called by a button ( OnClick="return Test();"  ) so i want to verify the result before continue
Before you continue with what? I suspect you meant "Before you continue to submit the form". If that is the case you do not need to do:
OnClick="return Test();"

instead you do:
OnClick="Test(); return false;"    

so that the submit button NEVER submits the form upon click. Instead you will "wait" for the call to completed and then if your condition is met, then you programatically submit the form.
function onSucceeded(result, userContext, methodName)
{
        MatchExist = result;
 
  if(MyResult == true){
            //blablabla...
   //assuming your html has <form name="myForm"...>
   document.forms.myForm.submit();
  } 
}

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article discusses how to implement server side field validation and display customized error messages to the client.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question