在 C# 中禁用浏览器的后退按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18009344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disabling backbutton of browser in C#
提问by Firoz Gandhi
<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>
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);
I have a master page in that logout button is there once user click on 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.
我在该注销按钮中有一个母版页,一旦用户单击该用户将被重定向到注销页面。这工作正常,一旦我单击后退按钮,它就会将我带到我浏览的最后一页。甚至我也尝试过使用 JavaScript。
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.
我正在创建 5 分钟后超时会话。当会话过期时,用户将被重定向到会话过期页面,还有后退按钮将我带到浏览的最后一页。
采纳答案by Mahesh
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
在这里,此 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>
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.
我们需要将上面的脚本放在页面的标题部分,我们需要防止用户使用浏览器后退按钮导航回另一个页面。
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.
我将用一个例子来解释我们的要求 我有两个页面 Defaul1.aspx 和 Default2.aspx 现在我将从 Default1.aspx 页面重定向到 Defaul2.aspx 页面。从 Defaul1.aspx 页面到 Default2.aspx 之后,如果我尝试从 Defaul2.aspx 导航回 Default1.aspx 页面,那么我想阻止用户导航回上一页 (Defaul1.aspx)。要实现此功能,请将所需页面的标题部分中的 JavaScript 函数置于上方。
After add our JavaScript functionality to our page that code will be like this
将我们的 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>
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.
我们也可以通过在后面的代码中禁用浏览器缓存来实现这一点,在 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();
}
We need to place this code in a page wherever we need to disable browser back button
我们需要将此代码放在需要禁用浏览器后退按钮的页面中
回答by salah9
<script type="text/javascript" language="javascript">
window.onload = function () {
noBack();
}
function noBack() {
window.history.forward();
}
</script>
<body onpageshow="if (event.persisted) noBack();">
</body>
Hello, You can do it like this,
你好,你可以这样做,
Implement this code in master page
在母版页中实现此代码
I have implemented this and it worked for me..
我已经实现了这个,它对我有用..
回答by Msoni
<script language="JavaScript">
this.history.forward(-1);
回答by Syed Mohamed
Redirect to Logout.aspx page on clicking "logout" Add a new page as Logout.aspx with following body content.
单击“注销”时重定向到 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>
add javascript as below
添加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
登出.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"));
}
Source : http://geekswithblogs.net/Frez/archive/2010/05/18/back-button-issue-after-logout-in-asp.net.aspx
来源:http: //geekswithblogs.net/Frez/archive/2010/05/18/back-button-issue-after-logout-in-asp.net.aspx
回答by Learner
When user clicks logout button, you should write single line to clear the session.
当用户点击注销按钮时,你应该写一行来清除会话。
Session.Abondon();
and navigate to logout page or login page. So that once user clicks logout button, he cannot go back becaause his session was cleared.
并导航到注销页面或登录页面。这样一旦用户单击注销按钮,他就无法返回,因为他的会话已被清除。
回答by shafi7468
To disable the back button of the browser write the below code at master page header part as
要禁用浏览器的后退按钮,请在母版页标题部分编写以下代码作为
<script language="JavaScript" type="text/javascript">
window.history.forward();
</script>
回答by JIYAUL MUSTAPHA
<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>