Asp.net prevents repeated submission during refresh and asp.net refresh submission

Source: Internet
Author: User

Asp.net prevents repeated submission during refresh and asp.net refresh submission

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.

Private bool pageRefreshed = false; // whether to refresh the page and submit private bool refreshState = false; // The temporary status in ViewState

Then rewrite the LoadViewState and SaveViewState methods of the Page:

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;}
private void Button1_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 address: http://www.codeproject.com/Articles/18841/Refresh-Module

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

<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

[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. It solves many problems.

Other methods are not listed one by one. The second method is simple and easy to use. All implementations are encapsulated for us and only need simple calls.

 

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.