views:

68

answers:

4

Hello, 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

A: 

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.

Paulie Waulie
Code behind is ok, I know how asp.net works, it seems some weird javascript problem.
opaera
+1  A: 

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 javacript 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)

Aristos
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).
opaera
It exsists just one time.
opaera
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.
Dienekes
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
@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)
Aristos
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.
opaera
A: 

I figured it out, the problem is disabling Button before returning true.

How could I make it work? I would like to disable button when postbacks.

(Sorry for not posting the code before)

opaera
If you mean from the server side then simply btnSave.Enabled = false.
Paulie Waulie
A: 

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 );
} 
lincolnk