ASP. NET prevents repeated data refresh (note)

Source: Internet
Author: User

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 method is as follows:Code(Post. aspx ):

// 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 ="* The title cannot be blank!";

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

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

Else

{

// Submit the data to the database.

// 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 ");

}

}

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.