Asp.Net method to prevent refresh of data submitted repeatedly

  • 2020-05-26 08:19:12
  • OfStack

Under 1 search on the net, you can find many information about this aspect, there are 1 piece is one kind of solution: from the MSDN http: / / msdn microsoft. com/library/default asp? url = / library en - us dnvs05 / html/BedrockASPNET asp by redefining it is System. Web. UI. Page class to load the page, is the "refresh" and "backward" request, or normal request, other pages are inherited the custom that a Page classes. I feel that this method of his is quite unique. Some examples can be downloaded and those interested can be studied.

Online most of the way to solve such problems is to not keep cache, which is submitted on the form data is not preserved by the browser's cache, if the refresh or back again request, will display "page has expired", data submitted, also won't repeat it played a stop refresh repeatedly submitting effect.

Here's a simple example of submitting a post, and how to disable caching to prevent repeated submissions from refreshing. The form data consists of two sections, "title" and "body."

Here is the code for this method (post.aspx) :


// Page load 
protected void Page_Load(object sender, EventArgs e)
{
   // You can set the page cache to" SetNoStore() ", that is, no cache 
   Response.Cache.SetNoStore();
   //Session Is stored in the" IsSubmit "Is to mark whether the submission was successful 
   if ((bool)Session["IsSubmit"])
   {
     // If the form data is submitted successfully, set" Session["IsSubmit"] "For the false
     Session["IsSubmit"] = false;
     // Displays the submission success information 
     ShowMsg.Text = " *  Submitted successfully !";
   }
   else
     // Otherwise (no submission, or page refresh), no information is displayed 
     ShowMsg.Text = "";
}
// The submit button (btnOK) Click event 
protected void btnOK_Click(object sender, EventArgs e)
{
   if (txtTitle.Text.ToString().Trim() == "")
     //ShowMsg Is used to display the prompt message 
     ShowMsg.Text = " *  The title cannot be empty !";
  else if (txtText.Text.ToString().Trim() == "")
     ShowMsg.Text = " *  The content cannot be empty !";
  else
   {
     // This is the submission of data to the database, omitted 
     /*
     string sql = "insert into tab...values(...)";
     MyConn.ExecQuery(sql);
     */
     // Upon successful submission, set" Session["IsSubmit"] "For the true
     Session["IsSubmit"] = true;
     // Cast the page (necessary, otherwise the refresh will still be repeated , Still on this page), 
      The submitted data in the cache is released through the page transformation, that is, the submitted bid data will not be saved into the cache. 
      If you backtrace, the page will appear that cannot be displayed 
     Response.Redirect("post.aspx");
  }
}

The above method is very simple and practical, I recommend you to use it.

Here's another method I developed myself, which is different from the "don't save cache" method, which is to let the browser save all the pages cached. This method USES random codes to determine whether the submission is normal or "refresh" or "backward".

First (the submission page is post.aspx), add the variable Rnd to Session to hold the random code, and instead of processing the form data, send the page to post.aspx? r=x, where "x" is equal to Session["Rnd"], this time when the page is loaded, by determining whether the value of r and Session["Rnd"] are the same, if the same, the data will be processed, otherwise it can be considered as "refresh" or "back" operation, and finally pay Session["Rnd"] a random code again.

Here is the method code (post.aspx) :


// Get random code 
public class MyRnd
{
   public static string Rnd()
   {
     // The random code is determined by  0-9 a-z A-Z  Composed of Numbers or letters 
     // So this is generated 20 A random code 
     //0..9 A..Z a..z
     //48-57 65-90 97-122
     string rst = "";
     Random rr = new Random();
     for (int i = 0; i < 20; i++)
     {
       int ir = 0;
       do
       {
         ir = rr.Next(123);
         if((ir >= 48) && (ir <= 57)) break;
         else if((ir >= 65) && (ir <= 90)) break;
         else if ((ir >= 97) && (ir <= 122)) break;
       }
       while (true);
       rst += ((char)ir).ToString();
       }
     return rst;
   }
}
// Page load 
protected void Page_Load(object sender, EventArgs e)
{
   // To obtain URL Requested" r "Value, if" r "Nonexistence rule  r=""
   string r = "";
   if(Request.QueryString["r"] != null)
     r = Request.QueryString["r"].ToString().Trim();
   string t;
   // To obtain   " Session "   In the   " Rnd "   Value used for and" r " 
   t = Session["Rnd"].ToString().Trim();
   // If the" r=t "Is the submission operation, which can process the data of the form 
  if(r == t)
  {
     if (txtTitle.Text.ToString().Trim() == "")
       ShowMsg.Text = " *  The title cannot be empty !";
     else if (txtText.Text.ToString().Trim() == "")
       ShowMsg.Text = " *  The content cannot be empty !";
     else      {
       // This is the submission of data to the database, omitted 
       /*
       string sql = "insert into tab...values(...)";
       MyConn.ExecQuery(sql);
       */
       // Clear the form data after a successful submission 
       txtTitle.Text = "";
       txtText.Text = "";
       // Displays the submission success information 
       ShowMsg.Text = " *  Submitted successfully !";
     }
  }
   // Otherwise, it can be considered a "refresh" or "back" operation 
   else
   {
       txtTitle.Text = "";
       txtText.Text = "";
  }
  // And finally get it back." Session["Rnd"] Is the value of ", and will btnOK.PostBackUrl Set to "" Session["Rnd"] "The value of the 
  Session["Rnd"] = MyRnd.Rnd();
  btnOK.PostBackUrl ="post.aspx?r=" + Session["Rnd"].ToString().Trim();
}
// So here's the submit button (btnOK) Clicking on the event eliminates the need to write any code 

In this way, each time to load the page "Session [" Rnd"] "will be a new value, and will not be the same when the refresh or back" r "and" t "values, the data also submitted will not be repeated, it is only through the operation of the" btnOK "to submit to get" r = = t ", data will be submitted to deal with, through the way of judging random code to prevent refresh duplicates can be achieved.


Related articles: