In Asp.net development, the browser refresh repeatedly triggers events, leading to repeated submission issues. Below are several solutions. I will make a simple analysis on the applicable situations and advantages and disadvantages.
Method 1: check the data table to see if the same data exists. This method may make more sense for inserting data, but it is troublesome to define the same data. In addition, if the data is not inserted or deleted, it is not applicable.
Method 2: After submission, go to a transition page and return to the current page from the transition page. This requires that the URL address of the current page be passed as a parameter after submission, and the cache cannot be saved. Otherwise, the "back" operation will lead to adverse consequences. However, it may be too difficult to simply delete the object.
Method 3: use JavaScript to capture F5 events. For example, the following code is provided:
| The code is as follows: |
Copy code |
Required parameter doc ument. onkeydown = KeyStroke; Function KeyStroke () { Var key = event. keyCode; Event. srcElement. releaseCapture (); If (key = 116) { Event. keyCode = 0; Event. returnValue = false; } } |
It seems good. F5 is disabled, but if JavaScript is disabled in the browser, what should I do if I use right-click to refresh it? Disable right-click? It is unreasonable not to disable this function to implement a function.
Method 4: process based on session and ViewState. For the best examples, see:
The principle of this method is to "write the current ViewState to a Session at the end of the page code execution, during page loading, the system checks whether the Session value is equal to the current ViewState value (in fact, the Session value happens to be in the previous state of ViewState). If the difference is not the same, it is a normal postback, if they are equal, the browser refresh ". this method is not bad, but I am a little worried about the use of so many server resources by session.
The code is as follows:
SubmitOncePage
SubmitOncePage is a base class inherited from System. Web. UI. Page for the above analysis. It is necessary to prevent the Page that refresh the committed data from inheriting from this base class. The source code is as follows:
| The code is as follows: |
Copy code |
Namespace myControl { /** // <Summary> /// Name: SubmitOncePage /// Parent class: System. Web. UI. Page /// Description: page extension class that solves the problem of repeated data submission caused by browser refreshing. /// Example: if (! This. IsRefreshed) ///{ //// Code ///} // Original: Cong Xingzi (cncxz) E-mail: cncxz@126.com /// </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 item
First, compile the source code of the SubmitOncePage class into a separate dll, and then perform the test as follows:
1. Create an asp.net web application;
2. Add the dll reference corresponding to the SubmitOncePage class;
3. Add a Label control (Label1) and a Button control (Button1) to webform1 );
4. Set Text of Label1 to 0;
5. Double-click Button1 to go to the codebehind view;
6. Modify the parent class of WebForm1 as SubmitOncePage and add the test code. The result is as follows:
| The code is as follows: |
Copy code |
Public class WebForm1: myControl. SubmitOncePage { Protected System. Web. UI. WebControls. Label Label1; Protected System. Web. UI. WebControls. Button Button1; Code generated by Web form designer # code generated by region Web form designer Override protected void OnInit (EventArgs e) { // // CODEGEN: This call is required by the ASP. NET Web form designer. // InitializeComponent (); Base. OnInit (e ); } /** // <Summary> /// The designer supports the required methods-do not use the code editor to modify /// Content of this method. /// </Summary> Private void InitializeComponent () { This. Button1.Click + = new System. EventHandler (this. Button#click ); } # Endregion Private void button#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. Press F5 to run. In the browser window, click Button1 several times consecutively, refresh the page several times, and then click Button1 several times;
8 bytes to the test project directory, open the.txt and B .txt files, you can see if (! This. IsRefreshed).
Method 5: Use ajax to submit the operation. Of course, there is no refresh problem from the source, because there is no postback, and there is no problem of preventing refresh and submission. Some partial questions.
Method 6: Pass the HTTP context object to the page for processing. This method is msdn to give a method, the principle I will not go into details, see the http://msdn.microsoft.com/zh-cn/library/ms379557 (VS.80). aspx. I think this method is good. It is more convenient to handle this problem according to the asp.net operating mechanism. So I recommend this method. Although the msdn article was an old article in 2004, it still seems quite good.