Stefan
Stefan

Reputation: 226

Javascript to execute code from .cs?

I have a search.aspx page, with which I want to do the following:

If the user closes this page(browser), I want to trigger some code frome the .cs. I have a script which is called on page close: HandleClose(). I can prompt an alert if I close the page, so I know the function HandleClose() is called. Now I want to execute to a method from the .cs (logout the user). I tried using the .click() on a button, which then would execute the code from the .cs.

I tried using "document.getElementById("buttonname").click()" in HandleClose(), but this doesnt work, it looks like the OnClick event isnt triggerd. Any idea how I can execute my code from the HandleClose() function?

Upvotes: 0

Views: 232

Answers (2)

Liam
Liam

Reputation: 29694

You'll need to make an asynchronous call to the code behind, probably using something like JQuery.

Baring that you could open a new window when the page is closed, do some code on Page_Load and then close this page after you've done what you need to do.

You can open a new page using window.open

Upvotes: 1

Vamsi
Vamsi

Reputation: 4253

You can use ajax with asp.net web methods to achieve what you are trying to do, but the page close event what ever you are trying is quite unreliable as the browser may not even give you enough time to fire the javascript in case of power failure or system error or something like that

Upvotes: 1

Related Questions