Asp.net prevents repeated submission during refresh (you can disable the toolbar refresh button)

Source: Internet
Author: User

Some time ago, when we met the need to disable refresh, The f5 button will not be mentioned, and simple js will be able to disable it, but the refresh button on the toolbar will not be able to do anything silly.

If you simply reload the screen during refresh, use window. location. href = "url" can be easily implemented, but the requirement is that you do nothing during refresh and keep the status of the screen. This can be complicated.

In asp.net, it is not very convenient to determine whether a request is a new request or another request by refreshing the button. In order to achieve this effect, we have tried many methods. The following two methods are used as an example:
1.
Copy codeThe Code is as follows:
Private bool pageRefreshed = false; // whether the page is refreshed and submitted
Private bool refreshState = false; // The State of temporary storage in ViewState

Then rewrite the LoadViewState and SaveViewState methods of the Page:
Copy codeThe Code is as follows:
Protected override void LoadViewState (object savedState)
{
Object [] states = (object []) savedState;
Base. LoadViewState (states [0]);
RefreshState = (bool) states [1];
If (Session ["_ PAGE_REFRESHED"] = null)
PageRefreshed = false;
Else
PageRefreshed = refreshState! = (Bool) Session ["_ PAGE_REFRESHED"];
}
Protected override object SaveViewState ()
{
Session ["_ PAGE_REFRESHED"] =! RefreshState;
Object [] states = new object [2];
States [0] = base. SaveViewState ();
States [1] =! RefreshState;
Return states;
}

Copy codeThe Code is as follows:
Private void button#click (object sender, EventArgs e)
{
If (pageRefreshed)
{
Label. Text = "this is refreshed function ";
}
Else
{
Label. Text = "this is new request function ";
}
}

Although this method can be implemented, it is not suitable for some payment requests. If both the text box and the button exist on the screen, when setting the button autopostback = "True", you can directly click the button after modifying the value of the text box (when the text box does not lose focus, click the button). The execution sequence is textchanged → buttonclick. When textchanged for the first time, the status has changed to true, and the button cannot be executed.

2. codeproject found another solution
This method can accurately determine whether a request is made through the refresh button of the browser, and it is very easy to use!
1. Reference dll and modify the configuration file
Add modules to the configuration file
Copy codeThe Code is as follows:
<System. web>
<HttpModules>
<Add name = "RefreshModule"
Type = "RefreshModule. Module, RefreshModule"/>
</HttpModules>
</System. web>

PS: in the case of wbapplication, you need to add modules to the modules node of system. webServer.
2. Define the behavior during refresh
Copy codeThe Code is as follows:
[Refresh ()]
Public partial class Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (IsPostBack &&! RefereshHelper. IsPageRefreshed)
{
// Do some work with the submitted date
}
Else
{
// Do some work when the page is loaded with the GET method
}
}
}

The RefereshHelper. IsPageRefreshed parameter is used to determine whether the request is made through the book refresh button in the browser. For more information about behavior control, see the original document. PS: codeproject is really a problem. Many problems are solved through it. Other methods are not listed one by one. The second method can be simple and easy to use, all the implementations have been encapsulated for us and only need simple calls.

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.