Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Handle Browser close event by pressing [X]

Handle Browser close event by pressing [X]

Scheduled Pinned Locked Moved ASP.NET
question
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    koolprasad2003
    wrote on last edited by
    #1

    button then i need to execute some code. I can do it using Page_Unload event but for that i need to repeate same code on each page. Can i catch the browser closing event in Gobal.ASAX... :confused: so i define code only once ....? Hope i clear what i want....... ;) Thanks in advance koolprasad2003

    Rating always..... WELCOME Be a good listener...Because Opprtunity knoughts softly...N-Joy

    S L B 3 Replies Last reply
    0
    • K koolprasad2003

      button then i need to execute some code. I can do it using Page_Unload event but for that i need to repeate same code on each page. Can i catch the browser closing event in Gobal.ASAX... :confused: so i define code only once ....? Hope i clear what i want....... ;) Thanks in advance koolprasad2003

      Rating always..... WELCOME Be a good listener...Because Opprtunity knoughts softly...N-Joy

      S Offline
      S Offline
      saini arun
      wrote on last edited by
      #2

      koolprasad2003 wrote:

      t but for that i need to repeate same code on each page

      If you are using master pages in your application, you wont have to write same code on each page. On other thought, you can create a base page and write code to inject javascript function using ScriptManager. And then make all pages to inherit the base class.

      1 Reply Last reply
      0
      • K koolprasad2003

        button then i need to execute some code. I can do it using Page_Unload event but for that i need to repeate same code on each page. Can i catch the browser closing event in Gobal.ASAX... :confused: so i define code only once ....? Hope i clear what i want....... ;) Thanks in advance koolprasad2003

        Rating always..... WELCOME Be a good listener...Because Opprtunity knoughts softly...N-Joy

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        There is no any permanent connection between your browser and the server. So you need to detect browser close event from client side (browser) and then call server event to execute your code. You can use master page to avoid writing repeating code on each page. Find following code for the same. 1. To enable page methods

        <asp:scriptmanager id="ScriptManager1" runat="server" enablepagemethods="true" />

        2. Detect browser close event.

        <body onunload="HandleClose()">

        3. Call pagemethods to invoke server side event.

        <script language="javascript" type="text/javascript">
        //<![CDATA[
        function HandleClose()
        {
        alert("Killing the session on the server!!");
        PageMethods.AbandonSession();
        }
        //]]>
        </script>

        4. Define your page methods at server side and call your code.

        [WebMethod]
        public static void AbandonSession()
        {
        HttpContext.Current.Session.Abandon();
        }

        HTH

        Jinal Desai - LIVE Experience is mother of sage....

        H 1 Reply Last reply
        0
        • K koolprasad2003

          button then i need to execute some code. I can do it using Page_Unload event but for that i need to repeate same code on each page. Can i catch the browser closing event in Gobal.ASAX... :confused: so i define code only once ....? Hope i clear what i want....... ;) Thanks in advance koolprasad2003

          Rating always..... WELCOME Be a good listener...Because Opprtunity knoughts softly...N-Joy

          B Offline
          B Offline
          Brij
          wrote on last edited by
          #4

          Have a look to trick http://www.codeproject.com/Tips/41771/How-to-detect-browser-close-when-browser-X-is-clic.aspx Hope it will help you

          Cheers!! Brij Check my latest Article :URL Routing with ASP.NET 4.0

          1 Reply Last reply
          0
          • L Lost User

            There is no any permanent connection between your browser and the server. So you need to detect browser close event from client side (browser) and then call server event to execute your code. You can use master page to avoid writing repeating code on each page. Find following code for the same. 1. To enable page methods

            <asp:scriptmanager id="ScriptManager1" runat="server" enablepagemethods="true" />

            2. Detect browser close event.

            <body onunload="HandleClose()">

            3. Call pagemethods to invoke server side event.

            <script language="javascript" type="text/javascript">
            //<![CDATA[
            function HandleClose()
            {
            alert("Killing the session on the server!!");
            PageMethods.AbandonSession();
            }
            //]]>
            </script>

            4. Define your page methods at server side and call your code.

            [WebMethod]
            public static void AbandonSession()
            {
            HttpContext.Current.Session.Abandon();
            }

            HTH

            Jinal Desai - LIVE Experience is mother of sage....

            H Offline
            H Offline
            Herman T Instance
            wrote on last edited by
            #5

            If you have a button or an imagebutton in the masterpage you can place the next code in you HandleClose() function: __doPostBack('<%= Button.UniqueID %>',''); or __doPostBack('<%= ImageButton.UniqueID %>',''); In that case you come in the OnClick event of the button or the imagebutton and there you can handle the Abandon of your Session

            In Word you can only store 2 bytes. That is why I use Writer.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups