实现了用户操作时 刷新导致的重复操作问题。 实现原理如下: 使用一个上下文唯一的票据号,该票据号随页面提交或回发而递增,每当用户与服务器端交互时,代码会提取上次的票据号码并与当前票据号码比较,如果当前票据大于上次的票据,则是提交或回发,否则则识别为刷新。 这种方式会用到HttpMoudel,并在其中操作Session。 使用方法如下: 1、引入dll MsdnExt 压缩包中有2.修改web.config,在
添加以下item:
其中“MsdnModule”是一个名字, “Msdn.RefreshModule”是“Msdn”下的“RefreshModule”类型,“MsdnExt”是程序集的名字 3.引入命名空间 using Msdn; 并修改页面类继承Msdn.Page 4.使用代码 代码: protected void Button1_Click(object sender, EventArgs e) { if (!IsPageRefresh) Response.Write("按钮事件"); else Response.Write("页面刷新");TrackRefreshState();} MSDNPage.cs 核心代码如下:using System;using System.Web.UI;using System.Web;using System.Text;namespace Msdn{ public class Page : System.Web.UI.Page { #region Constants // *********************************************************** // Constants public const string RefreshTicketCounter = "RefreshTicketCounter"; private const string SetFocusFunctionName = "__setFocus"; private const string SetFocusScriptName = "__inputFocusHandler"; // *********************************************************** #endregion // *********************************************************** // Ctor public Page() { m_focusedControl = ""; // Register a PreRender handler this.PreRender = new EventHandler(RefreshPage_PreRender); } // *********************************************************** // ************************************************************** // Indicates if the page is being viewed in response to F5 hit public bool IsPageRefresh { get { object o = HttpContext.Current.Items[RefreshAction.PageRefreshEntry]; if (o == null) return false; return (bool) o; } } // ************************************************************** // ************************************************************** // Increase the internal counter used to generate refresh tickets public void TrackRefreshState() { InitRefreshState(); int ticket = Convert.ToInt32(Session[RefreshTicketCounter]) 1; Session[RefreshTicketCounter] = ticket; } // ************************************************************** // ************************************************************** // Set the control with the input focus public void SetFocus(string ctlId) { m_focusedControl = ctlId; } // ************************************************************** #region Private Members // ************************************************************** // Create the hidden field to store the current request ticket private void SaveRefreshState() { int ticket = Convert.ToInt32(Session[RefreshTicketCounter]) 1; RegisterHiddenField(RefreshAction.CurrentRefreshTicketEntry, ticket.ToString()); } // ************************************************************** // ************************************************************** // Ensure that the ticket counter is initialized private void InitRefreshState() { if (Session[RefreshTicketCounter] == null) Session[RefreshTicketCounter] = 0; } // ************************************************************** // ************************************************************** // Handle the PreRender event private void RefreshPage_PreRender(object sender, EventArgs e) { SaveRefreshState(); AddSetFocusScript(); } // ************************************************************** // ************************************************************** // Add any script code required for the SetFocus feature private void AddSetFocusScript() { if (m_focusedControl == "") return; // Add the script to declare the function // (Only one form in ASP.NET pages) StringBuilder sb = new StringBuilder(""); sb.Append("