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
8
  • stackoverflow.com/questions/1824421/… Commented Jan 16, 2021 at 12:50
  • @mjwills I have tried but it is not working !! Commented Jan 16, 2021 at 12:55
  • <script type="text/javascript"> var closing = true; $(function () { $("a,input[type=submit]").click(function () { closing = false; }); $(window).unload(function () { if (closing) { jQuery.ajax({ url: "localhost/app/test.aspx", async: false }); } }); }); </script> In master page Commented Jan 16, 2021 at 12:57
  • 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 Commented Jan 19, 2021 at 4: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: stackoverflow.com/questions/1631959/… Commented Jan 19, 2021 at 4:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.