防止重複提交表單

來源:互聯網
上載者:User
public class RefreshServe : System.Web.UI.Page    {        private static ILog log = LogManager.GetLogger(typeof(RefreshServe));         private readonly string REFRESH_TICKET_NAME = "__RefreshTicketArray";        private readonly string HIDDEN_FIELD_NAME = "__RefreshHiddenField";        private readonly string HIDDEN_PAGE_GUID = "__RefreshPageGuid";         /// <summary>        /// 為True表示頁面重新整理,False為正常提交        /// </summary>        public bool IsPageRefreshed        {            get            {                if (IsPostBack && !CheckRefreshFlag())                {                    log.Debug("重新整理了頁面");                    return true;                }                else                {                    log.Debug("正常提交");                    return false;                }            }        }         /// <summary>        /// 呈現前更新標識        /// </summary>        /// <param name="e"></param>        protected override void OnPreRender(EventArgs e)        {            log.Debug("執行OnPreRender");            base.OnPreRender(e);            UpdateRefreshFlag();        }          /// <summary>        /// 更新標識,正常提交都刪除該次提交的時間,並生產當前新的時間        /// </summary>        private void UpdateRefreshFlag()        {             #region Cookie模式             //註冊頁面唯一標識並返回            string pageGuid = SetCurPageGuid();             HttpCookie cookie = GetRefreshTicket();                        if (cookie.Values.Count > 0)            {                cookie.Values.Remove(pageGuid);                log.Debug("當前清除的cookie變是:" + pageGuid);            }             string submitTime = DateTime.Now.ToString("hhmmss.fffff");            //當前提交時間儲存到隱藏欄位            ClientScript.RegisterHiddenField(HIDDEN_FIELD_NAME, submitTime);              log.Debug("即將要新增的時間:submitTime:" + submitTime + "  Guid:" + pageGuid.ToString());            cookie.Values.Add(pageGuid, submitTime);             log.Debug("UpdateRefreshFlag中當前Cookie中存在的記錄數為:" + cookie.Values.Count);            for (int i = 0; i < cookie.Values.Count; i++)                log.Info("cookie[" + cookie.Values.GetKey(i) + "]:" + cookie.Values[i]);             Response.AppendCookie(cookie);             #endregion         }          /// <summary>        /// 驗證是否重新整理        /// </summary>        /// <returns></returns>        private bool CheckRefreshFlag()        {            HttpCookie cookie = GetRefreshTicket();            string pageGuid = GetCurPageGuid();            if (cookie.Values.Count > 0)            {                bool flag;                if (cookie.Values[pageGuid] != null)                    flag = cookie.Values[pageGuid].IndexOf(GetCurSubmitTime()) > -1;                else                    flag = true;//防止出現異常,總是可以提交                if (flag)                    log.Debug("提交時間存在,可以提交");                else                    log.Debug("無效的提交時間");                return flag;            }            return true;        }          /// <summary>        /// 得到已儲存的提交時間,沒有建立,有返回        /// </summary>        /// <returns></returns>        private HttpCookie GetRefreshTicket()        {                        #region Cookie模式,傳回值為Cookie             HttpCookie cookie;            if (Request.Cookies[REFRESH_TICKET_NAME] == null)            {                cookie = new HttpCookie(REFRESH_TICKET_NAME);                Response.AppendCookie(cookie);                log.Debug("Cookie不存在,初始化");            }            else            {                cookie = Request.Cookies[REFRESH_TICKET_NAME];                 log.Debug("讀取已存在的Cookie,當前Cookie中存在的記錄數為:" + cookie.Values.Count + "具體有如下幾條:");                 for (int i = 0; i < cookie.Values.Count; i++)                    log.Info("cookie[" + cookie.Values.GetKey(i) + "]:" + cookie.Values[i]);            }            return cookie;            #endregion        }          /// <summary>        /// 擷取當前提交時間        /// </summary>        /// <returns></returns>        private string GetCurSubmitTime()        {            string submitTime = Request.Params[HIDDEN_FIELD_NAME] == null ? "" : Request.Params[HIDDEN_FIELD_NAME].ToString();            log.Debug("執行GetCurSubmitTime:submitTime為:" + submitTime);            return submitTime;        }          /// <summary>        /// 設定頁面唯一標識,通過Guid標識來區分每個頁面自己的提交時間        /// </summary>        private string SetCurPageGuid()        {            string guid;            if (!IsPostBack)            {                if (Request.Params[HIDDEN_PAGE_GUID] == null)                {                    guid = System.Guid.NewGuid().ToString();                    log.Debug("SetCurPageGuid註冊了一個新的標識:" + guid);                }                else                    guid = GetCurPageGuid();             }            else            {                guid = GetCurPageGuid();                           }             ClientScript.RegisterHiddenField(HIDDEN_PAGE_GUID, guid);            return guid;        }            /// <summary>        /// 得到當前頁面的唯一標識        /// </summary>        /// <returns></returns>        private string GetCurPageGuid()        {            string pageGuid = Request.Params[HIDDEN_PAGE_GUID] == null ? "none" : Request.Params[HIDDEN_PAGE_GUID].ToString();            log.Debug("執行GetCurPageGuid()後Page_GUID為:" + pageGuid);            return pageGuid;        }

 需要重新整理判斷功能時新頁面只需繼承該類就可,通過引用屬性IsPageRefreshed識別"為真表示重新整理,假則是正常提交",將資料庫的操作寫在

if (!IsPageRefreshed) {     資料庫操作 } 即可,如果是重新整理不會執行,代碼中注釋部分使用的是Session方式儲存票證,因為session比較容易丟失且占記憶體,所以使用cookie,

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.