How to find whether the user clicks browser back button or Refresh button
First of all, giving error messages if users use Back or have to refresh a page for whatever reason, is a really bad idea. Instead, you should transparently deal with that. Think about a page not coming up fully because of problems on the transportation level - the only option the user has is to reload or go back.
To answer your question, you have to keep track of the user's navigation yourself, that means on the server side. Forget about java-script here. If the user visits a website, you can store that information in a Session associated to the user (there are several methods of keeping these unique sessions, and I won't go into details here). If you store in your internal structures which pages the user visited lately, it is easy to determine a page being visited twice, or navigation going into the "wrong" direction.
You could easily generalize this (and making the whole thing more robust, for example against users jumping wildly between urls, or going back more than one step at once) by building a graph of "allowed" navigation and traversing it while the user is visiting websites.
The correct behaviour then if the user is doing a "wrong" navigation (like stepping back, reloading == visiting twice) is to get him back on track. Not to give an error message he can't escape! As he is not allowed to reload or go back, he has no options left.
Implement a PageToken using a guid/or timestamp stored in session and compare the value to a hidden field on the form. I did this via a PageToken Class. If the value from the hidden field and the session variable don't match then you're out of synch and you handle that. The trick is to map all events on your page.
public void GeneratePageToken()
{
SessionVariables currSession = new SessionVariables();
hfMasterPageToken.Value = System.DateTime.Now.ToString();
currSession.PageToken = hfMasterPageToken.Value;
}
public string GetLastPageToken
{
get
{
SessionVariables currSession = new SessionVariables();
return currSession.PageToken;
}
}
public bool TokensMatch
{
get
{
SessionVariables currSession = new SessionVariables();
return (currSession.PageToken != null
&& currSession.PageToken == hfMasterPageToken.Value);
}
}
In your Event method before your regular code:
if (this.TokensMatch == false)
{
//Reload the data.
//Generates a NewPageToken (this.GeneratePageToken();)
ReloadDataMethod();
this.MessageToUser =
"Data reloaded. Click Edit or Insert button to change.";
this.MessageType = MessageToUserType.Success;
this.DisplayMessageToUser = true;
return;
}
With Site.Master
In your Site.Master
, put after <body>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
<dx:ASPxLoadingPanel ID="MainLoadingPanel" runat="server" ClientInstanceName="MainLoadingPanel" Modal="True" />
<input type="hidden" id="refreshed" value="no" />
<script type="text/javascript" language="javascript">
MainLoadingPanel.Show();
window.onload = function () {
var e = document.getElementById("refreshed");
if (e.value == "no") {
MainLoadingPanel.Hide();
e.value = "yes";
} else {
e.value = "no";
location.reload(true); // Reload the page or redirect...
}
};
</script>
Without Site.Master
In your common code, put after <body>
<input type="hidden" id="refreshed" value="no" />
<script type="text/javascript" language="javascript">
window.onload = function () {
var e = document.getElementById("refreshed");
if (e.value == "no") {
e.value = "yes";
} else {
e.value = "no";
location.reload(true); // Reload the page or redirect...
}
};
</script>
You can't. The browser doesn't send it's own ui events to the server. All you get are http requests and one looks pretty much like another. Maybe the clicked the back button or maybe they just retyped the last url in. Tell us what problems it's causing and we can help you adapt your project to work with the http protocol a little better.