禁用浏览器的后退按钮在C# [英] Disabling backbutton of browser in C#

查看:148
本文介绍了禁用浏览器的后退按钮在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我用下面的code在母版页魔鬼后退按钮。

I am using the following code in the master page to diable the back button.

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);

我在注销按钮母版页有一次,该用户将被重定向到注销page.this工作正常用户clickon有一次我很单击后退按钮它带我到最后一页我browsed.Even我试图使用Javascript。

I have a master page in that logout button is there once user clickon that user will be redirected to logout page.this is working fine and once i am clicking on back button it is taking me to the last page i browsed.Even i tried with Javascript.

在5 minutes.when会话到期的用户会被重定向到会话过期页面也有被后退按钮带我去浏览的最后一页,我创建超时的会话。

I am creating timing out the session after 5 minutes.when session expires user will be redirected to session expiry page there also backbutton is taking me to the last page browsed.

推荐答案

下面这段JavaScript功能将在所有浏览器和prevent用户导航回到previous页的工作通过点击下面一块浏览器后退按钮检查JavaScript的code

Here this JavaScript functionality will work in all browsers and prevent users navigating back to previous page by hitting on browser back button check below piece of JavaScript code

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

我们需要将上述页面的标题部分脚本的地方,我们需要prevent用户通过浏览器后退按钮导航回到另一个页面。

We need to place above script in header section of a page wherever we need to prevent users navigate back to another page by using browser back button.

我将解释我们用一个例子来要求我有两页Defaul1.aspx和Default2.aspx现在我将从Default1.aspx页面重定向到Defaul2.aspx页面。来后从Defaul1.aspx页,Default2.aspx如果我尝试导航回到从Defaul2.aspx Default1.aspx页,然后我要prevent用户导航回到previous页(Defaul1.aspx)。为了达到上述JavaScript函数这一功能发生在需要的页面的页眉部分。

I will explain our requirement with an example I have two pages Defaul1.aspx and Default2.aspx now I will redirect from Default1.aspx page to Defaul2.aspx page. After come from Defaul1.aspx page to Default2.aspx if I try to navigate back to Default1.aspx page from Defaul2.aspx then I want prevent user navigate back to previous page (Defaul1.aspx). To achieve this functionality place above JavaScript function in header section of required page.

添加我们的JavaScript功能后,我们的页面,code会是这样

After add our JavaScript functionality to our page that code will be like this

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

我们也可以通过在code禁用浏览器缓存达到这个背后写code在Page_Init事件或Page_Load事件下面的线,也不要忘了添加使用System.Web命名;因为HttpCacheability与该命名空间。

We can also achieve this by disabling browser caching in code behind write the following lines of code in Page_Init event or Page_Load event and don’t forgot to add namespace using System.Web; because HttpCacheability related to that namespace.

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

我们需要把这个code在一个页面的地方,我们需要禁用浏览器的后退按钮

We need to place this code in a page wherever we need to disable browser back button

这篇关于禁用浏览器的后退按钮在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆