简体   繁体   English

防止页面被重定向

[英]preventing page from being redirected

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. 我知道其中一些可以使用Javacript完成,但是我遇到的问题实际上是停止重定向。 Here's what I have: 这是我所拥有的:

JavaScript: 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. 如果您使用的是传统的PostBacks,则浏览器已经开始请求并侦听新页面,并且没有“没关系”的HTTP响应。 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. 我认为如果您必须执行此验证/处理服务器端,则希望使用AJAX请求。

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 较新的信息: http : //msdn.microsoft.com/zh-cn/library/bb398864(v=vs.100).aspx

older link, but nice overview: http://www.asp.net/ajax/documentation/live/overview/updatepaneloverview.aspx 较旧的链接,但概述不错: http : //www.asp.net/ajax/documentation/live/overview/updatepaneloverview.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM