Asp.Net如何防止刷新

wsbgy 2010-12-08 04:02:35
请教:在页面中如何防止通过按F5或右键刷新页面?
CS代码或JavaScript代码都行,谢谢
...全文
211 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
蚂蚁上树 2010-12-09
  • 打赏
  • 举报
回复
提交后禁用按钮一定的时间 清空文本
CS_9413 2010-12-09
  • 打赏
  • 举报
回复

/// <summary>
/// 判断是否为刷新
/// </summary>
protected bool IsRefresh
{ get { return (Request.Headers["Accept"] == "*/*"); } }
cjh200102 2010-12-09
  • 打赏
  • 举报
回复
重定向页面
ltcszk 2010-12-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wsbgy 的回复:]

可能我得换种说法,刷新肯定还是需要刷新的,只是如何知道页面是刷新的呢?这样防止数据重复提交!
[/Quote]
重新定位,也就是提交成功后跳转到其他页面,这样post信息就清除了
ly520dreaming 2010-12-09
  • 打赏
  • 举报
回复
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>


</ContentTemplate>
</asp:UpdatePanel>
在页面上加上就ok了。
chen_ya_ping 2010-12-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wsbgy 的回复:]

可能我得换种说法,刷新肯定还是需要刷新的,只是如何知道页面是刷新的呢?这样防止数据重复提交!
[/Quote]
你提交了数据以后,清空你的文本框。试试。
chengfellow 2010-12-08
  • 打赏
  • 举报
回复
wsbgy 2010-12-08
  • 打赏
  • 举报
回复
可能我得换种说法,刷新肯定还是需要刷新的,只是如何知道页面是刷新的呢?这样防止数据重复提交!
deepmist 2010-12-08
  • 打赏
  • 举报
回复
看看这个


<%@ OutputCache Duration= "1 " VaryByParam= "None " %>
在前台引用此代码,当用户后退时会提示此页面已过期


SubmitOncePage:解决刷新页面造成的数据重复提交问题

执行过postback操作的web页面在刷新的时候,浏览器会有“不重新发送信息,则无法刷新网页”的提示,若刚刚执行的恰好是往数据库插入一条新记录的操作,点[重试]的结果是插入了两条重复的记录,以前一直是用保存数据后重新转向当前页面的方法解决,最近又找到了一个新的方法。

问题分析

在System.Web.UI.Page类中,有一个名为ViewState属性用以保存页面的当前视图状态,观察每个aspx页面最终生成的html代码可以发现,其实就是向页面添加了一个名为__VIEWSTATE的隐藏域,其value值就是页面的当前状态,每次执行postback过后,该value值都会发生变化,而刷新页面则不会改变。

针对这种情况,我们可以在页面代码执行的末尾将当前的ViewState写到一个Session中,而在页面加载时则判断该Session值是否与当前ViewState相等(其实Session值恰好是ViewState的前一状态),若不等,则是正常的postback,若是相等则是浏览器刷新,这样一来,只要在我们的数据插入代码外嵌套一个if判断就可以达到防止数据重复提交的目的了。

其实到这里问题还没有完全解决,具体说来就是Session的键值问题。假设我们将ViewState保存为this.Session["myViewState"],如果一个用户同时打开两个防刷新提交的页面就乱套了,那针对页面的url设置Session的键值呢?还是不行,因为用户有可能在两个窗口中打开同一页面,所以必须为每次打开的页面定义唯一的Session键值,并且该键值可以随当前页面实例一起保存,参考ViewState的保存方式,我们直接向页面添加一个隐藏域专门存放Session键值就可以了。

经oop80和Edward.Net的提醒,为了尽可能地降低Session数据对服务器资源的占用量,现将上述方案略做调整,将ViewState利用md5加密后返回的32位字符串写入Session。

另外,由于本方法会生成额外的Session占用服务器资源,所以请在必须保留当前页面状态的情况下使用,若无需保留当前页面状态,则在完成数据提交后直接重定向到当前页面即可。

SubmitOncePage

SubmitOncePage是针对上述分析写的一个继承自System.Web.UI.Page的基类,需要防止刷新重复提交数据的页面从该基类继承,源码如下:

namespace myControl
{
/// <summary>
/// 名称:SubmitOncePage
/// 父类:System.Web.UI.Page
/// 描述:解决浏览器刷新造成的数据重复提交问题的page扩展类。
/// 示例: if (!this.IsRefreshed)
/// {
/// //具体代码
/// }
/// 原创:丛兴滋(cncxz) E-mail:cncxz@126.com
/// </summary>
public class SubmitOncePage:System.Web.UI.Page
{
private string _strSessionKey;
private string _hiddenfieldName;
private string _strLastViewstate;

public SubmitOncePage()
{
_hiddenfieldName = "__LastVIEWSTATE_SessionKey";
_strSessionKey = System.Guid.NewGuid().ToString();
_strLastViewstate = string.Empty;
}

public bool IsRefreshed
{
get
{
string str1 = GetSessinContent();
_strLastViewstate = str1;
string str2 = this.Session[GetSessinKey()] as string;
bool flag1 = (str1 != null) && (str2 != null) && (str1 == str2);
return flag1;
}
}

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
string str = GetSessinKey();
this.Session[str] = _strLastViewstate;
this.RegisterHiddenField(_hiddenfieldName, str);
base.Render(writer);
}


private string GetSessinKey()
{
string str = this.Request.Form[_hiddenfieldName];
return (str == null) ? _strSessionKey : str;
}

private string GetSessinContent() {
string str = this.Request.Form["__VIEWSTATE"];
if (str == null) {
return null;
}
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
}

}
}

测试项目

首先将SubmitOncePage类的源码编译成一个单独的dll,然后进行测试,步骤如下:

1、新建一个asp.net web应用程序;
2、添加SubmitOncePage类对应的dll引用;
3、给webform1添加一个Label控件(Label1)和一个Button控件(Button1);
4、设置Label1的Text为0;
5、双击Button1转到codebehind视图;
6、修改类WebForm1的父类为SubmitOncePage并添加测试代码,结果如下:
public class WebForm1 : myControl.SubmitOncePage
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;


#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
int i=int.Parse(Label1.Text)+1;
Label1.Text = i.ToString();
if (!this.IsRefreshed)
{
WriteFile("a.txt", i.ToString());
}
WriteFile("b.txt", i.ToString());


}

private void WriteFile(string strFileName,string strContent)
{
string str = this.Server.MapPath(strFileName);
System.IO.StreamWriter sw = System.IO.File.AppendText(str);
sw.WriteLine(strContent);
sw.Flush();
sw.Close();
}
}


7、按F5运行,在浏览器窗口中连续点击几次Button1,然后刷新几次页面,再点击几次Button1;
8、转到测试项目对应目录下,打开a.txt和b.txt文件,可看到if (!this.IsRefreshed) 的具体效果。


binlan2008 2010-12-08
  • 打赏
  • 举报
回复
学习了,就JS就这样的
Iovswety 2010-12-08
  • 打赏
  • 举报
回复
屏蔽右键

<script>
var isNS = (navigator.appName == "Netscape") ? 1 : 0;
if(navigator.appName == "Netscape") document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);
function mischandler(){
return false;
}
function mousehandler(e){
var myevent = (isNS) ? e : event;
var eventbutton = (isNS) ? myevent.which : myevent.button;
if((eventbutton==2)||(eventbutton==3)) return false;
}
document.oncontextmenu = mischandler;
document.onmousedown = mousehandler;
document.onmouseup = mousehandler;
</script>
q107770540 2010-12-08
  • 打赏
  • 举报
回复

JavaScript屏蔽F5键
代码如下
<script language="javascript">
<!--
function document.onkeydown()
{
if ( event.keyCode==116)
{
event.keyCode = 0;
event.cancelBubble = true;
return false;
}
}
-->
</script>

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧