Some time ago encountered the need to disable the refresh needs, the F5 button is not said, simple JS can be disabled, but the toolbar refresh button is silly Shagan not off.
If the simple reload the screen in the refresh, through the window.location.href= "url" can be easily implemented, but the requirement is to do nothing in the refresh, keep the picture state, this can be complicated.
It is not convenient to asp.net whether a request is requested again or through a refresh button, in order to achieve this effect, try a lot of ways, one of the following two examples
1.
Copy Code code as follows:
private bool pagerefreshed = false; Whether the page refreshes the submit
private bool refreshstate = false; Status of staging in ViewState
Then rewrite the page's LoadViewState and SaveViewState methods:
Copy Code code 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 Code code as follows:
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 achieved, it is not adapted to some of the requests. If you have both text boxes and buttons on the screen, set the button's autopostback= "True", when you have finished modifying the value of the text box, click the button directly (click the button directly when the text box does not lose focus), The order of execution is Textchanged→textchanged→buttonclick, the first time textchanged, the state has become true, the button can not be performed.
2.codeproject found another way to fix it.
This method can accurately determine whether the browser's refresh button to make the request, but also very simple to use!
1. Referencing DLLs, modifying configuration files
Add modules to the configuration file
Copy Code code as follows:
<system.web>
<add name= "Refreshmodule"
Type= "Refreshmodule.module, Refreshmodule"/>
</system.web>
In the case of ps:wbapplication, it needs to be replaced under system.webserver modules node modules
2. Define behavior when refreshing
Copy Code code 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
}
}
}
Refereshhelper.ispagerefreshed This parameter is used to determine whether the browser through the book refresh button request. Other behavioral controls refer to the original. Ps:codeproject is really a place, a lot of problems are through it to solve the other way to enumerate, listed the second way can be said to be simple and easy to use, all of the implementation has been encapsulated for us, just want simple call.