The method has two, summarized as follows:
First method:
Knock directly in CS code:
Response.Buffer = true;
Response.ExpiresAbsolute = DateTime.Now.AddSeconds (-1);
Response.Expires = 0;
Response.CacheControl = "No-cache";
When someone wants to push back the page has expired, the effect is reached
Second method:
Submitoncepage: Solve the problem of duplicate data submission caused by refreshing the page (online data)
When a Web page that performs a postback operation is refreshed, the browser will have the prompt "do not resend the information, cannot refresh the Web page", and if you have just performed the action of inserting a new record into the database, the result of the point [retry] is inserting two duplicate records, It has always been a solution to the current page after saving data, and recently found a new method.
Problem analysis
In the System.Web.UI.Page class, there is a ViewState attribute to hold the current view state of the page, and to observe the resulting HTML code for each ASPX page can be found by adding a hidden field named __viewstate to the page. The value is the current state of the page, and the value changes after each execution of postback, and the Refresh page does not change.
In this case, we can write the current viewstate to a session at the end of the page code execution, while the page is loaded to determine whether the session value is the same as the current ViewState equal (in fact, the session value is exactly the first state of the ViewState), if unequal, it is normal postback, if the same is the browser refresh, so, It is possible to prevent data duplication by nesting an if judgment outside of our data insertion code.
In fact, the problem has not been fully resolved, specifically, the session of the key value problem. Let's say we save the viewstate as this. session["Myviewstate"], if a user at the same time open two anti-refresh submission of the page is out of order, that the page URL set session key value? Still not, because it is possible for a user to open the same page in two windows, you must define a unique session key value for each open page, and the key value can be saved with the current page instance, depending on how the viewstate is saved, We add a hidden field directly to the page to store the session key value.
After the OOP80 and edward.net reminders, in order to reduce the session data to the server resources as much as possible, the above scheme is adjusted slightly, will viewstate use MD5 encryption after the return of the 32-bit string write session.
In addition, because this method generates additional session Occupancy server resources, use if you have to keep the current page state, and then redirect directly to the current page when the data submission is complete without having to keep the current page state.
Submitoncepage
Submitoncepage is written for the above analysis of a inherited from the System.Web.UI.Page base class, need to prevent the refresh of repeated submission of data from the base class page inheritance, the source code is as follows:
Copy Code code as follows:
Namespace MyControl
{
<summary>
Name: Submitoncepage
Parent class: System.Web.UI.Page
Description: Page extension class that resolves data recurrence problems caused by browser refreshes.
Example: if (!this. isrefreshed)
///{
Specific code
///}
</summary>
public class SubmitOncePage:System.Web.UI.Page
{
private string _strsessionkey;
private string _hiddenfieldname;
private string _strlastviewstate;
Public Submitoncepage ()
{
_hiddenfieldname = "__lastviewstate_sessionkey";
_strsessionkey = System.Guid.NewGuid (). ToString ();
_strlastviewstate = string. Empty;
}
public bool Isrefreshed
{
Get
{
String str1 = Getsessincontent ();
_strlastviewstate = str1;
String str2 = this. Session[getsessinkey ()] As String;
BOOL Flag1 = (str1!= null) && (str2!= null) && (str1 = = str2);
return FLAG1;
}
}
protected override void Render (System.Web.UI.HtmlTextWriter writer)
{
String str = Getsessinkey ();
This. SESSION[STR] = _strlastviewstate;
This. RegisterHiddenField (_hiddenfieldname, str);
Base. Render (writer);
}
private String Getsessinkey ()
{
String str = this. Request.form[_hiddenfieldname];
return (str = null)? _STRSESSIONKEY:STR;
}
private String Getsessincontent () {
String str = this. request.form["__viewstate"];
if (str = null) {
return null;
}
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (str, "MD5");
}
}
}
Test Project
First compile the source code of the Submitoncepage class into a separate DLL, and then test the following steps:
1, a new asp.net Web application;
2, add the Submitoncepage class corresponding DLL reference;
3. Add a Label control (LABEL1) and a button control (Button1) to the WebForm1;
4, set the Label1 text is 0;
5, double-click Button1 go to Codebehind view;
6, modify the class WebForm1 the parent class is submitoncepage and add test code, the result is as follows:
Copy Code code as follows:
public class WebForm1:myControl.SubmitOncePage
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Button1.Click + = new System.EventHandler (this. Button1_Click);
}
#endregion
private void Button1_Click (object sender, System.EventArgs e)
{
int I=int. Parse (Label1.Text) +1;
Label1.Text = i.ToString ();
if (!this. isrefreshed)
{
WriteFile ("A.txt", i.ToString ());
}
WriteFile ("B.txt", i.ToString ());
}
private void WriteFile (String strfilename,string strcontent)
{
String str = this. Server.MapPath (strFileName);
System.IO.StreamWriter SW = System.IO.File.AppendText (str);
Sw. WriteLine (strcontent);
Sw. Flush ();
Sw. Close ();
}
}
7, according to F5 Run, in the browser window a number of consecutive clicks Button1, and then refresh several pages, and then click a few times Button1;
8, go to the test project corresponding directory, open a.txt and b.txt files, you can see if (!this. isrefreshed) of the concrete effect.