c# - 在 C# 中禁用浏览器的后退按钮

标签 c# asp.net

<script type="text/javascript">
    {
        function DisableBackButton() {
            window.history.forward()
        }

        DisableBackButton();

        window.onload = DisableBackButton;
        window.onpageshow = function (evt) { if (evt.persisted) DisableBackButton() }
        window.onunload = function () { void (0) }
    }
</script>

我在母版页中使用以下代码禁用后退按钮。

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();

Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
Response.Expires = -1500;
Response.CacheControl = "no-cache";
Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);

我有一个母版页,其中有一个注销按钮,一旦用户单击该用户,该用户将被重定向到注销页面。这工作正常,一旦我点击后退按钮,它就会带我到我浏览的最后一页。即使我尝试使用 JavaScript。

我在 5 分钟后创建 session 超时。当 session 过期时,用户将被重定向到 session 过期页面,那里还有后退按钮将我带到上次浏览的页面。

最佳答案

这里的 JavaScript 功能将适用于所有浏览器,并防止用户通过点击浏览器后退按钮检查下面的 JavaScript 代码片段而返回到上一页

<script type="text/javascript" language="javascript">
     function DisableBackButton() {
       window.history.forward()
      }
     DisableBackButton();
     window.onload = DisableBackButton;
     window.onpageshow = function(evt) { if (evt.persisted) DisableBackButton() }
     window.onunload = function() { void (0) }
 </script>

我们需要将上面的脚本放在页面的 header 部分,以防止用户使用浏览器后退按钮导航回另一个页面。

我将用一个例子来解释我们的要求 我有两个页面 Defaul1.aspx 和 Default2.aspx 现在我将从 Default1.aspx 页面重定向到 Defaul2.aspx 页面。从 Defaul1.aspx 页面转到 Default2.aspx 之后,如果我尝试从 Defaul2.aspx 导航回 Default1.aspx 页面,那么我想阻止用户导航回上一页 (Defaul1.aspx)。要实现此功能,请将 JavaScript 函数放在所需页面的 header 部分中。

将我们的 JavaScript 功能添加到我们的页面后,代码将如下所示

<html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
    <title>Disable Browser Back buttons</title>
    <script type="text/javascript" language="javascript">

      function DisableBackButton() {
       window.history.forward()
      }
      DisableBackButton();
       window.onload = DisableBackButton;
       window.onpageshow = function(evt) { if (evt.persisted) DisableBackButton() }
        window.onunload = function() { void (0) }
     </script>
   </head>
   <body >
    <form id="form1" runat="server">
     <div>
        First Page
    </div> 
      <div>
         <asp:Button id="btnFirst" runat="server" Text="Go to First Page"  PostBackUrl="~/Default.aspx"  />
        <asp:Button ID="btnSecond" runat="server" Text="Go to Second Page"  PostBackUrl="~/Default2.aspx" />
        <asp:Button ID="btnThree" runat="server" Text="Go to Third Page" PostBackUrl="~/Default3.aspx" />
       </div>
    </form>
    </body>
   </html>

我们还可以通过在代码后面禁用浏览器缓存来实现这一点,在 Page_Init 事件或 Page_Load 事件中编写以下代码行,并且不要忘记使用 System.Web 添加命名空间;因为 HttpCacheability 与该命名空间相关。

 protected void Page_Init(object sender, EventArgs e)
  {
      Response.Cache.SetCacheability(HttpCacheability.NoCache);
      Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
      Response.Cache.SetNoStore();
   }

我们需要将此代码放在需要禁用浏览器后退按钮的页面中

关于c# - 在 C# 中禁用浏览器的后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18009344/

相关文章:

asp.net - jquery checkboxlist 特定元素

c# - 每次触发事件时如何将 C# 变量值注册到 Javascript 变量

c# - 找不到类型或 namespace 名称 'WebControls'(是否缺少using指令或程序集引用?)

asp.net - 有关获取诊断数据转储的全局错误处理程序的建议

C# WinForms AnimateWindow 问题

c# - 检查属性的模型状态

c# - 通过 C 和/或汇编帮助优化 C# 函数

c# - 在运行时移动控制

c# - 为什么 XPath 表达式在 C# 中不返回任何结果,但在 C# 之外却返回任何结果?

asp.net - 来自根抛出异常的 ServiceStack REST API 路径变量