如何在aspx中禁用后退按钮

本文关键字:按钮 aspx | 更新日期: 2023-09-27 18:26:16

这是我的代码:

protected void btnresult_Click(Object sender, EventArgs e)
{
   Button btn = (Button)(sender);
   Response.Write("<script>");
   Response.Write("window.open('studentresult.aspx?id=" + btn.CommandArgument + "','_blank')");
   Response.Write("</script>");
}

我的主页studententry.aspx

在我的主页上,当我点击结果按钮时,它会转到新的窗口页面,并显示学生的结果。

但在那之后,我会看一看主页,它在url地址栏的顶部显示后退按钮。

所以我点击后退按钮,它再次进入新的窗口页面并显示学生的成绩。

我可以知道,为什么当我点击结果按钮时,后退按钮是活动的吗?

如何在aspx中禁用后退按钮

在页面代码隐藏文件中编写以下方法,并在页面加载事件中调用它。

public void disablebrowserbackbutton()
{
 HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
 HttpContext.Current.Response.Cache.SetNoServerCaching();
 HttpContext.Current.Response.Cache.SetNoStore();
}

<head>标签内的客户端文件(html)中添加以下脚本,或者如果您在contentPlaceHolder 之前使用Masterpages

<script type="text/javascript">
function preventBack() {
    window.history.forward();
}
setTimeout("preventBack()", 0);
window.onunload = function() {
    null
};