Prevent repeated forms

Source: Internet
Author: 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> // if true, the page is refreshed. If false, the page is submitted normally. // </Summary> Public bool ispagerefres Hed {get {If (ispostback &&! Checkrefreshflag () {log. debug ("refreshed page"); Return true;} else {log. debug ("normal submission"); Return false ;}}} /// <summary> /// the updated ID before rendering /// </Summary> /// <Param name = "E"> </param> protected override void onprerender (eventargs E) {log. debug ("execute onprerender"); base. onprerender (E); updaterefreshflag () ;}/// <summary> // update the ID. The submission time is deleted during normal submission, and generate the current time. // </Summary> private void updaterefreshflag () {# Region Cookie mode // uniquely identifies the registration page and returns string pageguid = setcurpageguid (); httpcookie cookie = getrefreshticket (); If (cookie. values. count> 0) {cookie. values. remove (pageguid); log. debug ("the current cleared cookie is changed to:" + pageguid);} string submittime = datetime. now. tostring ("hhmmss. fffff "); // Save the current submission time to the clientscript hidden domain. registerhiddenfield (hidden_field_name, submittime); log. debug ("time to be added: submittime:" + submittime + "guid :" + Pageguid. tostring (); cookie. values. add (pageguid, submittime); log. debug ("the number of records in the current cookie in updaterefreshflag is:" + 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> // verify whether refresh is performed // </Summary> // <returns> </returns> private bool checkrefreshflag () {H Ttpcookie 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; // to prevent exceptions, you can always submit if (FLAG) log. debug ("submission time exists, you can submit"); else log. debug ("invalid submission time"); Return flag;} return true ;}/// <summary> /// get the saved submission time, not created, returns /// </Summary> /// <returns> </returns> private httpcookie getrefreshticket () {# region cookie mode. The returned value is Cookie httpcookie; If (request. cookies [refresh_ticket_nam E] = NULL) {cookie = new httpcookie (refresh_ticket_name); response. appendcookie (cookie); log. debug ("Cookie does not exist, initialization");} else {cookie = request. cookies [refresh_ticket_name]; log. debug ("read existing cookies. The number of records in the current cookie is:" + Cookie. values. count + "there are the following items:"); For (INT I = 0; I <cookie. values. count; I ++) log. info ("Cookie [" + Cookie. values. getkey (I) + "]:" + Cookie. values [I]);} return cookie; # endregio N} /// <summary> /// obtain the current submission time /// </Summary> /// <returns> </returns> private string getcursubmittime () {string submittime = request. params [hidden_field_name] = NULL? "": Request. params [hidden_field_name]. tostring (); log. debug ("Run getcursubmittime: submittime:" + submittime); Return submittime ;}/// <summary> // set the unique page identifier, identify the submission time of each page by guid. // </Summary> private string setcurpageguid () {string guid; If (! Ispostback) {If (request. params [hidden_page_guid] = NULL) {guid = system. guid. newguid (). tostring (); log. debug ("setcurpageguid registers a new identifier:" + guid);} else guid = getcurpageguid ();} else {guid = getcurpageguid ();} clientscript. registerhiddenfield (hidden_page_guid, guid); Return guid ;} /// <summary> /// obtain the unique identifier of the current page /// </Summary> /// <returns> </returns> private string getcurpageguid () {string P Ageguid = request. Params [hidden_page_guid] = NULL? "None": request. Params [hidden_page_guid]. tostring (); log. debug ("after getcurpageguid () is executed, page_guid is:" + pageguid); Return pageguid ;}

 When you need to refresh the judgment function, the new page only needs to inherit this class. It can be identified by referencing the ispagerefreshed attribute."If it is true, it indicates refresh. If it is false, it indicates normal submission"Write database operations in

if (!IsPageRefreshed) {     Database Operations } If it is refreshed, it will not be executed. in the comments section of the Code, the session method is used to save the ticket. Because the session is easy to lose and occupies memory, the cookie is used,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.