0

I use a webmethod to identify whether the user can "Delete a record".

Here is the JavaScript code before adding this access control.

$(".apply-delete-msg").live('click', function() {
   return confirm("Are you sure you want to delete this item(s)?);    
});

Now it will call the webMethod to validate the access

<WebMethod(EnableSession:=True)> _
Public Function CanAccess() As Boolean
    Return ServerCode.IsAccessable
End Function

The new JavaScript then:

$(".delete-msg").live('click', function() {    

    MPWebService.CanAccess(
        //B
        function(boolRes) {    

          if (boolRes == true){
                 return  confirm("Are you sure you want to delete this item(s)?");}             
          else{
                 alter("can't access");
                 return false;
          }           

    });

    // **Here is Comment A**: Return true/false

});

I want the ".Live" method to return a true/false if the user can access and confirm delete/cannot access or cancel the delete.

But if i'm right the method will call CanAccess first, and then Comment A:, finally Comment B, i.e. function(boolRes).

Because the value of boolRes is inside function(boolRes) and it is processed at last, it's tricky for me to get a return value from this method at Comment A position.

Any suggestions?

2 Answers 2

1

The Web Service call is asynchronous and will return a result after you have returned from the click message. Therefore you always need to return false from the click method and re-design your page, so that the action that would have normally taken place after client side click (and true returned) to be performed after web service method returns. It would also be good to have a "Please wait..." message, while waiting for the call to complete.

Sign up to request clarification or add additional context in comments.

4 Comments

make sense. but if the click event always return false, how does it fire the delete function in service. Call the server side event in JS?
Could u give me a simple example for this?
Do you have a post-back action associated with the .delete-msg click? You probably have and this is the action that you want to initiate after the web method call. Return false will guarantee that the post-back won't be called from the click event handler. You can make the post-back using doPostBack (See this question for example: stackoverflow.com/questions/2738327/…)
It seems doesn't work. I put e.preventDefault() in the outer function and e.trigger in inner, it doesn't the server-side-action. Then I put return true in outer, e.preventDefault() in inner, it still call the server-side-action, mb is not a good place to use webmethod, can it be non-asynchronous?
0

There is two ways to do it :

  • do the comment A code into the boolRes==true block
  • create a callback which contains the code of comment A and call this callback in the boolRes==true block

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.