We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Detect Close Browser in ASP.NET

Thread ID:

Created:

Updated:

Platform:

Replies:

108924 May 21,2013 02:55 PM UTC May 28,2013 12:43 PM UTC ASP.NET Web Forms (Classic) 1
loading
Tags: Tools
Hayssam Hassan
Asked On May 21, 2013 02:55 PM UTC

I have a ASP.NET web app with a MasterPage and contents page, from the MasterPage i define a menuItems like "Home", "New" and "Logout". I click  the "New" MenuItem to open a new aspx Page. if I want to close the new page browser tab, I want to show a popup or a dialog that alert the user that he/ she is closing the browser tab. I used the following code in the new aspx page:

 <script type="text/javascript">
        var warnMessage = "Are sure you want to close Application?";
        window.onbeforeunload = function () { return warnMessage };
        $(document).ready(function () {
            $('a').click(function () { window.onbeforeunload = null; });
            $('form').submit(function () { window.onbeforeunload = null; });
        });
    </script>
the problem is that if i press also other buttons or menuItems than the browse closeTab the method works. i would like to know how can i avoid it. i think i need to detect the Syncfusion MenuItem event but i dont know how.

Sunil Prabakar C [Syncfusion]
Replied On May 28, 2013 12:43 PM UTC

Hi Hayssam Hassan,

 

Thanks for using Syncfusion products.

 

To avoid showing alert on clicking menuitem or button we have maintained a flag variable in "Menu" control’s cliendSideevent "ClientSideOnItemSelect"

and "Button" control’s cliendsideevent "OnClientClick" .Therefore, alert will be shown only if the flag is false.

 

Please refer the following code snippet

 

<code>

[JavaScript]

 

<script type="text/javascript">

        var event = 0;       //falg variable

        function buttonClick() {

            event = 1;                //set falg true

        }

        function menuClick() {

            event = 1;                //set flag true

        }

        var warnMessage = "Are sure you want to close Application?";

        window.onbeforeunload = function () {

            if (event === 0) { 

                return warnMessage

            }

 

        };

        $(document).ready(function () {

            $('a').click(function () { window.onbeforeunload = null; });

            $('form').submit(function () { window.onbeforeunload = null; });

        });

    </script>

 

 

[Aspx]

 

  <syncfusion:Menu ID="Menu" runat="server" ClientSideOnItemSelect="menuClick()">

  <asp:Button ID="btn" runat="server" Text="Button Sample" OnClientClick="return buttonClick()" />

 

</code>

 

We have also prepared a simple sample to showcase this behavior and the sample can be downloaded from the link given below

 

MenuDemo.zip

Kindly try the attached sample  and let us know if this helps.

 

Thanks & Regards

Sunil Prabakar C

 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;