【问题标题】:Disabling backbutton of browser in C#在 C# 中禁用浏览器的后退按钮
【发布时间】:2013-08-03 06:41:02
【问题描述】:
<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 分钟后创建会话超时。当会话到期时,用户将被重定向到会话到期页面,并且后退按钮会将我带到浏览的最后一页。

【问题讨论】:

标签: c# asp.net


【解决方案1】:

此 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>

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

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

将我们的 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();
   }

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

【讨论】:

  • 嗨,maddy,该代码适用于两页,但是当母版页进入图片时,它无法正常工作。
  • 工作,但上一页在 ASP.NET MVC 中闪烁/闪烁一秒钟
【解决方案2】:
<script type="text/javascript" language="javascript">
    window.onload = function () {
        noBack();
    }
    function noBack() {
        window.history.forward();
    }
</script>
<body  onpageshow="if (event.persisted) noBack();">
</body>

你好, 你可以这样做,

在母版页中实现此代码

我已经实现了这个,它对我有用..

【讨论】:

  • 我将这段代码放在母版页中,我的后退按钮仍将我带到最后浏览的页面,应用程序中的另一件事后退按钮应该可以工作,并且在注销页面和会话到期页面返回按钮应该不行。这是要求。
  • 在注销页面和到期页面中插入这段代码......并且这段代码在我的应用程序中运行良好
【解决方案3】:
<script language="JavaScript">
this.history.forward(-1);

【讨论】:

    【解决方案4】:

    单击“注销”时重定向到 Logout.aspx 页面 将新页面添加为 Logout.aspx,并包含以下正文内容。

            Please wait. You are Logging out.
            <asp:ScriptManager ID="ScriptManager1" runat="server">  
            </asp:ScriptManager>  
            <asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">  
            </asp:Timer>
    

    如下添加javascript

    function noBack() {
                window.history.forward()
            }
            noBack();
            window.onload = noBack;
            window.onpageshow = function (evt) { if (evt.persisted) noBack(); }
            window.onunload = function () { void (0); }
    

    Logout.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();  
        }
    
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            Response.Redirect("Login.aspx")); 
        }
    

    来源:http://geekswithblogs.net/Frez/archive/2010/05/18/back-button-issue-after-logout-in-asp.net.aspx

    【讨论】:

      【解决方案5】:

      当用户点击注销按钮时,你应该写一行来清除会话。

      Session.Abondon();
      

      并导航到注销页面或登录页面。这样一旦用户点击注销按钮,他就无法返回,因为他的会话被清除了。

      【讨论】:

        【解决方案6】:

        要禁用浏览器的后退按钮,请在母版页标题部分编写以下代码

        <script language="JavaScript" type="text/javascript">
        window.history.forward();              
        </script>
        

        【讨论】:

          【解决方案7】:
           <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>
          

          【讨论】:

            猜你喜欢
            • 2011-08-06
            • 2010-10-31
            • 1970-01-01
            • 2014-04-17
            • 2014-04-09
            • 2022-10-25
            • 2011-04-06
            相关资源
            最近更新 更多