I need to find whether the user clicking the browser back button or Refresh button.
I need to redirect the page to Error page when he clicks the back or refresh button.
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.
This simply detects if the user lands on your page using the back/forward buttons... the only problem is with IE8 and lower, the browser version cannot handle this.
if(performance.navigation.type == 2)
{
//Do your code here
}
I presume you want to do this because of post-back to avoid resubmitting some action or data right? ironically, this wasn't as huge of a problem back before asp.net webforms ... because you generally had to post to a different URL instead of the same one (much like asp.net mvc actually).
One trick I liked to use ... even though in the most technical of points is slower ... was to have an input page, and a post page. The HTML output of the post page was some super minimal HTML with a tiny javascript that replaced the current url (of the post page) in the browser's history with the result page (or even the original referring page if you really wanted). The result was when the user would click the back button, he'd be sent to the original input page, or if he clicked refresh, he'd already be on the new result page.
<html><body onload="location.replace('someURL.asp')"></body></html>
We did it in Java/Struts1 by putting a property on the submit button. If the submit button was clicked the button text would be sent in the property in the ActionForm. If the user refreshed the information the value wasn't set, so we knew the user had not clicked the button. In this case where the property was empty, we went back and rendered the first page of the pageflow. YMMV in ASP.Net.
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;
}
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>
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>