0

I wanted to fire unload event when page is close or close browser tab / window. I have used below code but it is firing when page it load instead of page unload. I have used master / child page concept in asp.net

Partial Class test
    Inherits System.Web.UI.Page

    Dim dbconn As Object = New DBConnection()

    Private Sub test_Unload(sender As Object, e As EventArgs) Handles Me.Unload
        dbconn.OpenConnection()
        Dim cmd As SqlCommand = New SqlCommand("UPDATE test SET abc = '2' where sno = '123'", dbconn.con)
        cmd.CommandType = CommandType.Text
        cmd.ExecuteNonQuery()
        dbconn.CloseConnection()
    End Sub

End Class
Rakesh
  • 19
  • 6
  • https://stackoverflow.com/questions/1824421/detect-browser-close-on-asp-net – mjwills Jan 16 '21 at 12:50
  • @mjwills I have tried but it is not working !! – Rakesh Jan 16 '21 at 12:55
  • In master page – Rakesh Jan 16 '21 at 12:57
  • @mjwills any support ? – Rakesh Jan 17 '21 at 08:34
  • Page un-load fires for each page post + lifecycle. User hits post, page goes up to server. Page load and code behind runs - page is sent back down to browser - page un-load fires and THEN the page is tossed out and DOES NOT exist on the server anymore. Remember that web pages are state-less. once that post back occurs, code behind runs, and then the web page is sent down to the browser. At that point, the page class, values and page does NOT exist anymore on the server. The web site is simply sitting there waiting for ANY user to post back. The page does NOT exist server side anymore – Albert D. Kallal Jan 18 '21 at 03:55
  • @AlbertD.Kallal I just want to fire some event, when browser or tab is closed – Rakesh Jan 19 '21 at 04:06
  • 1
    Well, as noted, the code, and the web page does NOT exist server side. You can un-plug your computer or whatever - server has zero clue. The web pages are NOT in memory nor do they remain loaded on the server (and that includes any code variables etc.). The web server has NO pages loaded. it is just sitting their waiting for a web page to be posted back. then the code form class is created, code runs, and web page is sent back to browse and once again the server side web page is 100% gone and does NOT exist. So, you have to fire an event from the client browser side. Server page does not exist – Albert D. Kallal Jan 19 '21 at 04:41
  • 1
    So, you have to use client side code - there are no server side events since after that page is rendered and sent to browser - the server simply does not have or even KNOW about that web page existing. You thus require client side code. how to deal with this is explained here: https://stackoverflow.com/questions/1631959/how-to-capture-the-browser-window-close-event – Albert D. Kallal Jan 19 '21 at 04:45

0 Answers0