ASP. NET prevents refresh and repeated submission of data note (from the Internet, the specific source does not remember, and its content has not been put into practice, and the income will be added for the moment)

Source: Internet
Author: User

Recently, I encountered a problem when I used ASP. NET to write something: that is, after a user submits a form, the user will submit data repeatedly after pressing refresh, that is, the so-called "refresh and submit" problem. Search on the Internet, you can find a lot of information about this, one of which is a solution from msdn: http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/dnvs05/html/bedrockaspnet. ASP. web. UI. when loading a page, it is a "refresh", "back" request, or a normal request. Other pages inherit the custom page class. I feel that this method is quite unique. For example, you can download it. If you are interested, you can study it.

The most common solution to this problem on the Internet is not to save the cache, that is, after the submission, the data in the form will not be cached and saved by the browser. If you encounter a refresh or rewind request again, the page will display "the web page has expired", and the data will not be submitted repeatedly, which is effective in preventing refresh and repeated submission.

The following uses a simple post as an example to describe how to disable cache to prevent repeated submission refresh. The form data includes two parts: "title" and "body.

The code for this method (post. aspx) is as follows ):

// Page loading

Protected void page_load (Object sender, eventargs E)

{

// When loading a page, you can set the page cache to "setnostore ()", that is, no cache.

Response. cache. setnostore ();

// The variable "issubmit" stored in the session indicates whether the submission is successful.

If (bool) session ["issubmit"])

{

// If the form data is submitted successfully, set "session [" issubmit "]" to false.

Session ["issubmit"] = false;

// Display the submitted information

Showmsg. Text = "* submitted successfully! ";

}

Else

// Otherwise (no submission or page refresh), no information is displayed

Showmsg. Text = "";

}

// Click the event button (btnok ).

Protected void btnok_click (Object sender, eventargs E)

{

If (txttitle. Text. tostring (). Trim () = "")

// Showmsg is used to display the prompt information

Showmsg. Text = "* Title cannot be blank! ";

Else if (txttext. Text. tostring (). Trim () = "")

Showmsg. Text = "* content cannot be blank! ";

Else

{

// Submit the data to the database.

/*

String SQL = "insert into tab... values (...)";

Myconn. execquery (SQL );

*/

// After the submission is successful, set "session [" issubmit "]" to true

Session ["issubmit"] = true;

// Forcibly convert the page (not required, otherwise refresh will be submitted again and still go to this page ),

The data submitted in the cache is released through page conversion, that is, the submitted bidding data is not saved to the cache,

If you move back, the page cannot be displayed.

Response. Redirect ("post. aspx ");

}

}

The above method is very simple and practical. We recommend that you use it.

The following is another method I have developed. This method is different from the "do not save cache" method. It allows the browser to save all the page caches. This method uses random code to determine whether the request is submitted normally, refreshed, or backed up.

First (the submission page is post. aspx) adds the variable RND in the session to store random code, and does not process the data when submitting the form. Instead, it transfers the page to post. aspx? R = x. Here "X" is equal to session ["RND"]. When loading a page, you can determine whether the R value is the same as the session ["RND"] value, if the data is the same, the submitted data will be processed. Otherwise, it will be regarded as "refresh" or "back", and a random code will be sent to session ["RND"] again.

The code for this method (post. aspx) is as follows ):

// Obtain the random code

Public class myrnd

{

Public static string RND ()

{

// Random code is composed of digits or letters between 0-9 A-Z A-Z

// Below is the generated 20-bit random code

// 0 .. 9 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 loading

Protected void page_load (Object sender, eventargs E)

{

// Obtain the "r" value of the request in the URL. If "R" does not exist, r = ""

String r = "";

If (request. querystring ["R"]! = NULL)

R = request. querystring ["R"]. tostring (). Trim ();

String T;

// Obtain the "RND" value in "session" for comparison with "R"

T = session ["RND"]. tostring (). Trim ();

// If "r = T", the data in the form can be processed by submitting the operation.

If (r = T)

{

If (txttitle. Text. tostring (). Trim () = "")

Showmsg. Text = "* Title cannot be blank! ";

Else if (txttext. Text. tostring (). Trim () = "")

Showmsg. Text = "* content cannot be blank! ";

Else {

// Submit the data to the database.

/*

String SQL = "insert into tab... values (...)";

Myconn. execquery (SQL );

*/

// Clear the form data after the submission is successful

Txttitle. Text = "";

Txttext. Text = "";

// Display the submitted information

Showmsg. Text = "* submitted successfully! ";

}

}

// Otherwise, it can be regarded as a "refresh" or "back" operation.

Else

{

Txttitle. Text = "";

Txttext. Text = "";

}

// Obtain the value of "session [" RND "]" again, and set "btnok. postbackurl" to the value of "session [" RND "]".

Session ["RND"] = myrnd. RND ();

Btnok. postbackurl = "post. aspx? R = "+ session [" RND "]. tostring (). Trim ();

}

// The submit button (btnok) does not need to write any code when you click the event.

In this way, each time a page is loaded, "session [" RND "]" returns a new value, the same "R" and "T" values will not be obtained during refresh or rollback, and the data will not be submitted repeatedly, only operations submitted through "btnok" will get "r = T", and the data will be submitted for processing, you can stop refresh and commit by checking the random code.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.