Have seen other people to prevent the Refresh method, is to let the page refresh or return to the previous step to let the page expire, here introduces an alternative method, using session to deal with.
Implementation principle:
Because the refresh submission form is actually submitted in the last normal form, we just have to make a flag to determine whether a new form or a previous old form can tell if a repeat commit has been made.
Implementation method:
Place a hidden field on the page, and when the page is first loaded, save a flag in the session, and save the logo to the hidden on the page. When submitting a form, determine whether the hidden that are submitted in the form and the flags in the session are the same, you can tell if the form is being submitted correctly, or if the page is a recurring one. It should be noted that after each submission of the form's processing, update the flags in the session.
Code instance: The code is small, first on the page.
<%@ Page language= "C #" autoeventwireup= "true" codebehind= "Default.aspx.cs" inherits= "Test.Web.Default"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Places to look for:
The 1 GetToken () function is to obtain the flags that are saved in the session.
2 Hidden uses a non-server control, because I use the server control, and in the background directly to get the session flag and assign to this hidden, the refresh submitted to the server in the form of the hidden value also changed, guess is the server control, The values in the form are kept in sync, and, of course, I'm probably using the wrong method, quack.
The following is the background code:
Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.Security.Cryptography;
Using System.Text; namespace Test.web {public partial class Default:System.Web.UI.Page {protected void Page_Load (object sender,
EventArgs e) {//When first loaded, generate an initial flag if (null = session["Token"]) {Settoken (); } protected void button1_click (object sender, EventArgs e) {if (Request.Form.Get ("Hiddentestn").
Equals (GetToken ())) {lblmessage.forecolor = System.Drawing.Color.Blue;
Lblmessage.text = "normal Submission form";
Settoken ()//Don't forget to update the flag in the session at last {lblmessage.forecolor = System.Drawing.Color.Red;
Lblmessage.text = "Refresh Submit Form";
}//Get the flag saved in the current session the public string GetToken () {if (null!= session["Token"]) {return session[' Token '].
ToString (); else {return string.
Empty; }//Generate flags and save to session private void Settoken () {Session.add ("Token", UserMd5 (Session.SessionID + Da
TeTime.Now.Ticks.ToString ())); }//This function is purely to make the logo a little bit shorter, a bunch of garbled and mysterious, in addition, this USERMD5 function is found on the Internet ready-made protected string UserMd5 (String str1) {string
CL1 = str1;
string pwd = ""; MD5 MD5 = MD5.
Create ();
After encryption is an array of byte types byte[] s = Md5.computehash (Encoding.Unicode.GetBytes (CL1)); Converts an array of byte types to a string by using a loop, which is a regular character-formatted gain for (int i = 0; i < s.length; i++) {//The resulting string is formatted with hexadecimal type. The formatted character is a lowercase letter, and if uppercase (X) is used, the character in the format is pwd = pwd + s[i].
ToString ("X");
return pwd; }
}
}
Places to look for:
1 when the page is first loaded to generate a flag, the future will not be.
2 at the end of the function that the form handles, remember to update the flag.
3 Logo I chose the current SessionID plus the current time millisecond value, so that the basic can avoid the mark repeat, after a MD5, purely to make the logo short, of course, a little security meaning, haha.
All the code is these, very simple, do not know is because too simple or everyone has a better way, I did not find similar code on the Internet, so write down and share with you, if there is a better way, hope to tell me, because for a long time do not do web development, even if there are many new technologies will not.