3

I have a button that looks like:

<asp:Button ID="btnSave" runat="server" CssClass="SubmitButton" Text="Order >>" OnClientClick="return SaveChanges();" OnClick="btnSave_Click" />

A javascript function that looks like:

function SaveChanges() {
    var success = false;

    var arr1 = new Array();
    var arr2 = new Array();
    var arr3 = new Array();
    for (var i = 0; i < self.frames.length-1; i++) {
        arr1[i] = document.getElementById("frame" + i.toString()).contentWindow.getFramePSFID();
        arr2[i] = document.getElementById("frame" + i.toString()).contentWindow.getFrameHeading();
        arr3[i] = document.getElementById("frame" + i.toString()).contentWindow.saveSvg();
    }

    Scripts.WebServices.WSCommon.SaveChanges(arr1, arr2, arr3, function (result) {
        return true;
    });
}

The block where return true; is, is what happens once the web service call (which takes a few seconds) has finished and returned a result. Regardless of what I put there, btnSave_Click always triggers. (If I remove btnSave_Click and change return true; to alert('test');, the alert fires.).

What do I need to do to force btnSave_Click ONLY to fire once the web service has returned true?

1

2 Answers 2

0

function in your webservice call is a callback method to be executed when the asynchronous call returns..

but your SaveChanges method will not wait till then.

I think one way around you can do this is either have a hidden field in your webform or add an extra(your own) attribute to your input button.. set its value to some meaningful value in the callback method.. before calling the webService check this value.. if found return true, else call your webService and return false.

And also add code to click your button using either jQuery or javascript in the callback method instead of returning true..

something like this

function SaveChanges() {

   //your array code..

    if (<check_your_hidden_field_value>){
        return true;
    }else{
        Scripts.WebServices.WSCommon.SaveChanges(arr1, arr2, arr3, function (result) {

            //set your hiddenfield data here..

            document.getElementById('<%= btnSave.ClientID %>').click();//click button
        });
        return false;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

-1

How about a little tweak to SaveChanges() :

function SaveChanges() {
    var success = false;

    var arr1 = new Array();
    var arr2 = new Array();
    var arr3 = new Array();
    for (var i = 0; i < self.frames.length-1; i++) {
        arr1[i] = document.getElementById("frame" + i.toString()).contentWindow.getFramePSFID();
        arr2[i] = document.getElementById("frame" + i.toString()).contentWindow.getFrameHeading();
        arr3[i] = document.getElementById("frame" + i.toString()).contentWindow.saveSvg();
    }

    Scripts.WebServices.WSCommon.SaveChanges(arr1, arr2, arr3, function (result) {
        success = true;
        btnSave_Click();
    });

    return success;
}

4 Comments

btnSave_Click() is handled server-side
it will not work, it will allways return false, you have to declare success out of the scope of SaveChanges function
@user982119 I think Yohanes' solution is almost fine. Just a small change. Scripts.WebServices.WSCommon.SaveChanges(arr1, arr2, arr3, function (result) { success = true; document.getElementById('<%= btnSave.ClientID %>').click(); }); if(!success) { return false; } else { success = false; return true; }
Appreciate the comment but it doesn't solve the problem. What you suggested creates an endless loop.

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.