0

Here is my code :

<script type="text/javascript">
$(document).ready(function () {
    $(window).bind('beforeunload', function () {
        alert("unload");
        if (closeIt())
            $("#<%=Button1.ClientID %>").trigger('click');

    });
    function closeIt() {
        var ans = confirm("save current layout ?");
        if (ans) return true;

    }
});
</script> 
<asp:Button ID="Button1" runat="server" OnClick="btnSaveState_Click" style="display:none;" />

the new problem is that the confirm message is displayed twice on firefox and non of chrome

Sora
  • 2,465
  • 18
  • 73
  • 146
  • have you cheked if actually the confirm is setup correctly try putting like alert to see if it firing – COLD TOLD Aug 23 '12 at 06:29
  • 1
    instead `OnClick` modify `OnClientClick` in `button` property. – Krunal Mevada Aug 23 '12 at 06:30
  • if the confirm message is confirmed the btnSaveState_Click is fired but he's executing the confirm message twice and i can't find the issue and 'Krunal' i can't use onClientClick because i want to execute a server side function – Sora Aug 23 '12 at 06:39
  • my question is edited please check – Sora Aug 23 '12 at 07:32

2 Answers2

1

All the beforeunload handler is really supposed to do is return a string, which the browser then displays in an "are-you-sure-you-want-to-leave" dialog. If the user clicks OK, whatever navigation was about to happen occurs; if they click Cancel, it doesn't. Take a look here for more detail.

The usual thing to do here would be to display a message (by returning a string, not calling confirm yourself) along this lines of "You're about to lose the changes you've made to the current layout; are you sure you want to leave?" and then let the user themselves click Cancel and then Save, or OK if they don't care.

You're having issues, I expect, because you're trying to perform a postback in the handler, and the postback itself would cause an unload. I wouldn't be surprised if the browser deliberately stops this kind of behaviour in the handler, because malicious sites could use it to stop you leaving.

As per your update: only some browsers even allow beforeunoad behaviour. By the looks of it, Firefox does, so you get two dialogs - your confirm and then the browser default one. And by the looks of it, Chrome doesn't, so your event never gets called. (Or maybe Chrome just ignores the event if it does something unexpected, like post back.)

Rawling
  • 49,248
  • 7
  • 89
  • 127
  • than you mean i should not use a confirm message ? – Sora Aug 23 '12 at 07:46
  • No, don't use a confirm - use something like `window.onbeforeunload = function() { return "You're about to lose the changes you've made to the current layout; are you sure you want to leave?"; };` and the _browser_ will display that string in its own confirmation dialog. – Rawling Aug 23 '12 at 07:49
  • ok but then how can i trigger my server side click event of Button1 ? if the user click on OK – Sora Aug 23 '12 at 07:52
  • If the user click on OK, they've decided not to save their changes. If they click Cancel, they'll have a chance to save their changes manually, and the message you use should make that obvious. – Rawling Aug 23 '12 at 07:54
  • but like u see the save button has the display:none; so it's not visible on my page so the user can't save it's change manually for that reason i'm triggering it's click event – Sora Aug 23 '12 at 07:56
  • Ah, I hadn't noticed that, sorry. I don't think there's a way you can do this if the user can't even see the save button. – Rawling Aug 23 '12 at 08:00
  • than i guess i should find another way to do this maybe using a tooltip or something other than just a confirmation box – Sora Aug 23 '12 at 08:07
-1

Try this:

<asp:Button ID="Button1" runat="server" Text="Button"  CausesValidation="False" onclick="btnSaveState_Click"  onclientclick="return confirm('Are you sure you want to delete?')" />

Alternatively you can look at the ASP.Net Ajax <ajaxToolkit:ConfirmButtonExtender>

Conrad Lotz
  • 8,200
  • 3
  • 23
  • 27
  • Weird, this should work work fine. Did you try it exactly with the `btnSaveState_Click` in the code behind? – Conrad Lotz Aug 23 '12 at 06:58
  • What other controls do you have on the page? Do you have validators? – Conrad Lotz Aug 23 '12 at 07:01
  • my browser is executing the confirm message twice like when i click ok the first time a second confirm message appear – Sora Aug 23 '12 at 07:03
  • This code causes a confirm to appear when you click the button, before allowing the postback. It won't help trigger the postback on page-unload at all. – Rawling Aug 23 '12 at 07:23