Search the Internet for a lot of information about this, one of which is a workaround from MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ Dnvs05/html/bedrockaspnet.asp It is by redefining the System.Web.UI.Page class to implement a "refresh", "back" request, or a normal request when loading a page, and other pages inherit the custom page class. Feel him this method is more unique, there are examples can download, interested can study.
The most common way to solve such problems online is to not save the cache, that is, the data on the form after the submission is not saved by the browser's cache, and if you encounter a refresh or fallback request at this point, the page has expired and the data will not be repeated, which prevents the refresh from recurring submissions.
The following is a simple example of submitting a post as a way to disable caching to prevent refreshing duplicate submissions, and the form data includes the two sections "title" and "Body".
The following is the code for the Method (post.aspx):
Copy Code code as follows:
Page load
protected void Page_Load (object sender, EventArgs e)
{
You can set the page's cache to "Setnostore ()" When the page loads, that is, no caching
Response.Cache.SetNoStore ();
The variable "issubmit" stored in the session is marked with a successful
if ((bool) session["Issubmit"])
{
If the form data is submitted successfully, set "session[" Issubmit "]" to False
session["Issubmit"] = false;
Show Submission Success Information
Showmsg.text = "* submitted successfully!";
}
Else
Otherwise (no submission, or page refresh), no information is displayed
Showmsg.text = "";
}
Submit button (Btnok) Click event
protected void Btnok_click (object sender, EventArgs e)
{
if (txtTitle.Text.ToString (). Trim () = = "")
ShowMsg is used to display the hint information.
Showmsg.text = "* Title cannot be empty!";
else if (txtText.Text.ToString (). Trim () = = "")
Showmsg.text = "* Content cannot be empty!";
Else
{
Here is the submission of data to the database, omitting
/*
String sql = "INSERT into tab ..." VALUES (...) ";
Myconn.execquery (SQL);
*/
After the commit succeeds, set "session[" Issubmit "]" to True
session["Issubmit"] = true;
Cast page (not less, or refresh will still be repeated submit, still go to this page),
The data that is submitted in the cache is released through the conversion of the page, that is, the submitted form data is not saved in the cache.
If you go back, the page will not appear
Response.Redirect ("post.aspx");
}
}
The above method is very simple also very practical, recommended that you use.
Here's another approach I've developed for myself, unlike the "Do Not save Caching" method, which allows the browser to save all page caches. This method can be used to judge whether the normal commit or "refresh" or "back" is done by means of random code.
First (Submit page is post.aspx) in the session to increase the variable Rnd used to store random code, while submitting the form data does not do processing, but let the page to Post.aspx?r=x, where "x" Equals session["Rnd"], this time in the page load , by determining whether the value of R and session["Rnd"] is the same, if the same processing of the submitted data, it can be considered "refresh" or "back" operation, and finally pay session["Rnd" a random code.
The following is the method code (post.aspx):
Copy Code code as follows:
Get Random Code
public class Myrnd
{
public static string Rnd ()
{
Random code is made up of numbers or letters between 0-9 A-Z and a-Z.
Here is the generated 20-bit 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 < i++)
{
int ir = 0;
Todo
{
IR = RR. Next (123);
if (IR >= && (ir <=)) break;
else if (IR >=) && (ir <=)) break;
else if (IR >=) && (IR <= 122)) break;
}
while (true);
RST + + ((char) IR). ToString ();
}
return rst;
}
}
Page load
protected void Page_Load (object sender, EventArgs e)
{
Gets the "R" value requested in the URL, if "R" does not exist, r= ""
string r = "";
if (request.querystring["R"]!= null)
r = request.querystring["R"]. ToString (). Trim ();
String T;
Gets the "Rnd" value in "Session", which is used to compare with "R"
t = session["Rnd"]. ToString (). Trim ();
If "r=t" is a commit operation, the data for the form can be processed
if (r = = t)
{
if (txtTitle.Text.ToString (). Trim () = = "")
Showmsg.text = "* Title cannot be empty!";
else if (txtText.Text.ToString (). Trim () = = "")
Showmsg.text = "* Content cannot be empty!";
else {
Here is the submission of data to the database, omitting
/*
String sql = "INSERT into tab ..." VALUES (...) ";
Myconn.execquery (SQL);
*/
Empty form data after commit success
Txttitle.text = "";
Txttext.text = "";
Show Submission Success Information
Showmsg.text = "* submitted successfully!";
}
}
Otherwise, it can be considered a "refresh" or "back" action
Else
{
Txttitle.text = "";
Txttext.text = "";
}
Finally, to regain the value of "session[" Rnd "]" and set "Btnok.postbackurl" to the value of "session[" Rnd "]"
session["Rnd"] = Myrnd.rnd ();
Btnok.postbackurl = "post.aspx?r=" + session["Rnd"]. ToString (). Trim ();
}
Here the Submit button (Btnok) Click the event without having to write any code
In this way, the "session[" Rnd "]" will get a new value each time the page is loaded. The same "R" and "T" values will not be used when refreshing or receding, and the data will not be submitted repeatedly, and only actions submitted through "Btnok" will be "r==t". Data will be submitted for processing, by judging the way the random code to prevent the refresh duplicate commit can be achieved.