asp.net to prevent repeated submissions of of at refresh disable the toolbar refresh button

  • 2020-05-24 05:25:20
  • OfStack

Some time ago, when I encountered the need to disable refresh, the f5 button was ignored, and the simple js could disable it, but the refresh button on the toolbar was still silly.

If you simply reload the screen at refresh, it can be easily done by window.location.href ="url", but the requirement is to do nothing at refresh and keep the state of the screen, which can be complicated.

In asp.net, it is not very convenient to tell whether a request is rerequested or rerequested by the refresh button. In order to achieve this effect, many ways have been tried
1.
 
private bool pageRefreshed = false; // Whether the page is submitted as a refresh  
private bool refreshState = false; //ViewState The temporary state in  

Then rewrite Page's LoadViewState and SaveViewState methods:
 
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 under some requests. If there are both text box and button on the screen, when setting the value of the button autopostback="True", click the button directly after modifying the value of the text box (click the button directly when the text box does not lose focus), then the execution order is textchanged→textchanged→buttonclick. In the first textchanged, the state has become true and the button cannot be executed.

2. codeproject found another solution
This method can accurately determine whether the request is made through the browser's refresh button, and it is very simple to use!
1. Modify the configuration file by referring to dll
Add modules to the configuration file
 
<system.web> 
<httpModules> 
<add name="RefreshModule" 
type="RefreshModule.Module, RefreshModule"/> 
</httpModules> 
</system.web> 

In the case of PS: wbapplication, it needs to be changed to append modules under the modules node of system.webServer
2. Define the behavior at 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 parameter RefereshHelper.IsPageRefreshed is used to determine whether the request is made through the browser's book refresh button. Other behavioral controls refer to the text. PS: codeproject is really a place where many problems are solved in other ways. The second way is simple to use. All the implementations have been encapsulated for us and only need simple calls.

Related articles: