0

My problem is after I click a button in update panel and close the window, "beforeunload" event not fire, while when I click another button this event fires properly. Here is my code:

function dontlogout() //for postback refreshes
{
    postback = true;
    alert(postback);
}
$(window).bind('beforeunload',function() {

    if(postback==false)
    {   
       //my logout code here;

    }

    else
    {
        postback = false;
     }
    });  
<asp:Button runat="server" ID="cmdOut"  OnClientClick="dontlogout();" Text="next"  OnClick="cmdOut_Click"></asp:Button>

<asp:UpdatePanel runat="server" ID="updQuestion"   >
    <ContentTemplate>                         
<asp:Button runat="server" ID="cmdNext"  OnClientClick="dontlogout();"      Text="next" OnClick="cmdNext_Click"></asp:Button>

 </ContentTemplate></asp:UpdatePanel> 

when i click cmdOut button no problem and when close browser my log out code executed, but when i click cmdNext in update panel and then close the browser my log out code not executed.
How can I solve this problem?

saeid6366
  • 33
  • 7
  • Possible duplicate of [window.onbeforeunload not working](http://stackoverflow.com/questions/7255649/window-onbeforeunload-not-working) – Abdennour TOUMI Feb 19 '17 at 18:35

2 Answers2

0

You have to return in the function callback of onbeforeunload

$(window).bind('beforeunload',function() {

    if(postback==false)
    {   
       //my logout code here;

      //  Ok you can leave because you dont return
    }

    else
    {
        postback = false;
        return  "You cannot leave";  // You cannot leave
     }
    });  
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
0

Add the 'Triggers' tag just before the end tag of the update panel

<asp:UpdatePanel runat="server" ID="updQuestion"   >
    <ContentTemplate>                         
<asp:Button runat="server" ID="cmdNext"  OnClientClick="dontlogout();"      Text="next" OnClick="cmdNext_Click"></asp:Button>

 </ContentTemplate>
 <Triggers>
       <asp:PostBackTrigger ControlID="cmdNext" />
  </Triggers>
</asp:UpdatePanel> 
norbert
  • 29
  • 6