.net中防止刷新页面重复提交数据的几种方法
1.提交数据后,对页面进行重定向(可以重定向到本页或其他页面)。例子:
Response.Write( " <script language=javascript> alert( '操作成功 ');window.location.href=window.location.href; </script> ");
2.利用Session,第一次提交数据时把Session设置一个值,如果重复提交的话判断这个Session是否有值存在就可以了。
3.区分是:刷新还是提交(摘抄)
DetectRefresh.aspx
===========================
<%@ Page Inherits= "StevenBey.Web.UI.Page " %>
<html>
<head>
<title> Detecting Page Refresh [Demo] </title>
</head>
<body>
<form runat= "server ">
<asp:button Text= "Test Refresh " runat= "server " />
</form>
IsRefresh = <%= IsRefresh %>
</body>
</html>
StevenBey.Web.UI.Page.cs
======================
namespace StevenBey.Web.UI
{
public class Page : System.Web.UI.Page
{
private bool _refreshState;
private bool _isRefresh;
public bool IsRefresh
{
get
{
return _isRefresh;
}
}
protected override void LoadViewState(object savedState)
{
object[] allStates = (object[]) savedState;
base.LoadViewState(allStates[0]);
_refreshState = (bool) allStates[1];
_isRefresh = _refreshState == (bool) Session[ "__ISREFRESH "];
}
protected override object SaveViewState()
{
Session[ "__ISREFRESH "] = _refreshState;
object[] allStates = new object[2];
allStates[0] = base.SaveViewState();
allStates[1] = !_refreshState;
return allStates;
}
}
}