ASP防止页面刷新重新提交数据的方法

Response.Write("<script language='JavaScript'>alert('弹出窗口!');window.location='a.aspx';</script>");

    比如在1.aspx页面上点按钮,弹出来一个提示窗口,同时又希望按F5刷新的时候,不要重复提交,这个时候,把上面这段代码写到按钮的click事件里,把a.aspx换成1.aspx页面就行了。
    这里的方法实际上就是弹出窗口后重新指向原来的页面,如果是用Response.Redirect()方法的话,虽然可以实现指向新的页面的功能,但是就不会弹出窗口提示了。

http://blog.csdn.net/kgdiwss/archive/2005/08/04/445846.aspx

转,孟老大的解决方法

如何区分刷新和提交?

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

展开阅读全文

没有更多推荐了,返回首页