I was wondering if anyone knew a way to prevent a page from being redirected? I would like to capture an event when the page is leaving, then run some code behind. If the code behind meets certain criteria, then allow the redirect, otherwise cancel it. I know some of this can be accomplished with Javacript but the problem I have is actually stopping the redirect. Here's what I have:
JavaScript:
<script type="text/javascript">
window.onbeforeunload = confirmExit;
function clickButton(getId) {
var getBtn = document.getElementById(getId)
getBtn.click()
}
function confirmExit(e) {
var btnId = document.getElementById("<%=btnConfirmExit.ClientId %>").id
clickButton(btnId)
}
</script>
Code behind:
Protected Sub btnConfirmExit_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnConfirmExit.Click
'if something, cancel request
'else
' let it go
end Sub
If tried using:
Page.Response.End()
But this does nothing. Anyone have any ideas?
Thanks
If your are using classic postBacks, the browser has already begun requesting and listening for a new page, and there is no 'nevermind' HTTP response. Even if you got this to work on some browsers in some circumstances, it would be inconsistent and unreliable.
I think you want to look into using an AJAX request if you have to do this validation/processing server side.
I'm not a fan of them, but worst case, down and dirty and without changing much code, you can use an Update Panel. Read about it here:
newer info: http://msdn.microsoft.com/en-us/library/bb398864(v=vs.100).aspx
older link, but nice overview: http://www.asp.net/ajax/documentation/live/overview/updatepaneloverview.aspx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.