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?

notAnonymousAnymore
  • 2,637
  • 9
  • 49
  • 74
  • See this question: http://stackoverflow.com/questions/7610871/how-to-trigger-an-event-after-using-event-preventdefault – karaxuna Dec 03 '12 at 08:03

2 Answers2

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;
    }
}
techBeginner
  • 3,792
  • 11
  • 43
  • 59
-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;
}
Yohanes Gultom
  • 3,782
  • 2
  • 25
  • 38
  • btnSave_Click() is handled server-side – notAnonymousAnymore Dec 03 '12 at 08:06
  • it will not work, it will allways return false, you have to declare success out of the scope of SaveChanges function – karaxuna Dec 03 '12 at 08:07
  • @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; } – Teddy Dec 03 '12 at 09:33
  • Appreciate the comment but it doesn't solve the problem. What you suggested creates an endless loop. – notAnonymousAnymore Dec 03 '12 at 10:26