2

I have a strange problem, it seems, that I don't know how to solve. I have a button like this:

<asp:Button ID="btnSave" runat="server" ClientIDMode="Static" Text="Save" OnClientClick="return ConfirmSave();" OnClick="btnSave_Click" />

If I write my client function like the following, it works as expected:

function ConfirmSave()
{
  return confirm('Confirm?');
}

But I should check, inside the function, for the confirm result, like this:

    function ConfirmSave() 
    {
      if (Page_ClientValidate('validationGroup'))
      {
        var conf = confirm("Confirm?");

        if (conf)
        {
             $('#btnSave').attr('disabled', 'disabled');
        }

        return conf;
      }
      else
        return false;
   }

Doing this, the page postback but the server click event doesn't fire.

Anyone could help? Thanks

2
  • ^^ In the if condition, kindly replace "-- do something" with what is being done actually. Commented Aug 31, 2010 at 8:30
  • Use this code onclientclick it may help: <asp:Button ID="btnSave" runat="server" ClientIDMode="Static" Text="Save" OnClientClick="if(!ConfirmSave() return false;" OnClick="btnSave_Click" /> Commented Jul 22, 2015 at 7:49

3 Answers 3

2

use a timeout to disable the button immediately after your code completes and the submit happens.

if (conf) 
{ 
     setTimeout( function() { $('#btnSave').attr('disabled', 'disabled') }, 0 );
} 
Sign up to request clarification or add additional context in comments.

1 Comment

+1 this was the only thing that worked for me, apparently the webforms postback javascript decides not to process the button if the button is set to disabled by my script.
2

Yes its fires because the Button is input with submit type.

You probably have some other error, maybe a duplicate of your button id, in the page (because you have set it on static mode).

Check if you have give the btnSave again somewhere else, or check also if you have any JavaScript error.

(After your page is render, on your browser see the source code of your page and search for the btnSave id if exist more than one time)

5 Comments

If I have a duplicate button id the first Function I typed should not work, instead it works as expected. It seems with the first method it returns True/False correctly, nor in the second. It seems there are no javascript errors, but I'm not too sure because the page postback (without firing the server click).
Aristos is correct, there must be something else which is causing this behaviour. It doesn't looks to be directly related to the if conditional block.
So the first Method should not work, instead it works as expected. So it seems the only different thing is how the client function is written.
@opaera From the moment that you see the post back from this input, the btnSave_Click is called, except if you change something on your script (maybe you make a custom post back? change the submit id ... I am not sure)
Nothing seems to solve this. If I make a [return confirm()] it works, in debug mode I intercept the onclick server event. If I type that like [var conf = coonfirm(); if(conf) { return true; }] the page postback but the click event is not fired.
0

If you have a method called btnSave_Click() in your code behind then when you post back it will fall into your Page_load method first and then fall into that method. If you are using VB then you will need a method with Handles btnSave_Click() after it.

1 Comment

Code behind is ok, I know how asp.net works, it seems some weird javascript problem.

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.