Check F5 refresh submit post back repeatly


<%@ Page language="c#" AutoEventWireup="true"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>Check F5 refresh submit post back repeatly</title>
  <meta name="generator" content="editplus" />
  <meta name="author" content="" />
  <meta name="keywords" content="" />
  <meta name="description" content="" />
 <script language="C#" runat="server">
 private bool _IsF5RefreshPostBack;
 protected void Page_Load(object sender, EventArgs ea) 
 {
  if (IsPostBack)
  {
   if (Request.Cookies["cookieTimeStamp"] != null)
   {
    //if (Request.Cookies["cookieTimeStamp"].Value != this.label1.Text)
    //不用ViewState 使用 HiddenField 也可
    if (Request.Cookies["cookieTimeStamp"].Value != (string) ViewState["vsTimeStamp"])
    {
     _IsF5RefreshPostBack = true;
    }
   }
  }
  string timeStamp = DateTime.Now.ToString();
  //this.label1.Text = timeStamp;
  ViewState["vsTimeStamp"] = timeStamp;
  HttpCookie cookie = new HttpCookie("cookieTimeStamp", timeStamp);
  Response.Cookies.Add(cookie);
  
  if (_IsF5RefreshPostBack)
  {
   Response.Write("IsRefreshPostBack(F5) in Page Load<br>" );
  }
 }
 void button1_Click (object sender, EventArgs ea) 
 {
  if (_IsF5RefreshPostBack)
  {
   Response.Write("IsRefreshPostBack(F5) in Button Click<br>" );
  }
 }
</script>
 </HEAD>
 <body>
  <form id="Form1" method="post" runat="server">
    <asp:Button ID="button1" Text="button1" onclick="button1_Click" runat="server" />
  </form>
 </body>
</HTML>

posted @ 2008-10-14 22:07  于斯人也  阅读(3308)  评论(0编辑  收藏  举报