ASP.NET - how to disable browser back button using asp.net

Asked By theiry henry on 08-Jun-09 03:21 AM
how to disable browser back button using asp.net?

RE

Ravenet Rasaiyah replied to theiry henry on 08-Jun-09 03:50 AM
Hi

Yes you can do by two ways

1. Javascript

add this  script in your web page head(html).
<script type = "text/javascript" >
function disableBackButton()
{
window.history.forward(-1);
}
</script

2. by C#

protected void Page_Init(object Sender, EventArgs e)

{

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));

Response.Cache.SetNoStore();

}

thank you

try this

cool adch replied to theiry henry on 08-Jun-09 05:36 AM
Just put this javascript on the html section of aspx page above head section

<script type = "text/javascript" >
function disableBackButton()
{
window.history.forward();
}
setTimeout("disableBackButton()", 0);
</script>


We need to put it on the html section of the page which we want to prevent user to visit by hitting the back button

Complete code of the page looks like this
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type = "text/javascript" >
function disableBackButton()
{
window.history.forward();
}
setTimeout("disableBackButton()", 0);
</script>
</head>
<body onload="disableBackButton()">
<form id="form1" runat="server">
<div>
This is First page <br />
<br />
Go to Second page
<br />
<br />
<asp:LinkButton ID="LinkButton1" runat="server"
PostBackUrl="~/Default2.aspx">Go to Second Page
</asp:LinkButton></div>
</form>
</body>
</html>


If you want to disable back button using C# code, than you need to write this code either in Page_load event or in Page_Init event

In other words we need to disable browser caching by writing this line of code

protected void Page_Init(object Sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
Response.Cache.SetNoStore();
}
Doing this,user will get the page has expired message when hitting back button of browser


try this

cool adch replied to theiry henry on 08-Jun-09 05:37 AM
 

To avoid the displaying of page on click of back button we have to remove it from cache, or have to tell the server not to cache this page.

So if we do not cache the page then on click of back button request goes to server side and validation can be done whether session exist's or not.

 

This can be achieved by adding following code in the page load of the page for which we do not want to cache the page in history.

 

Response.Buffer=<SPAN style="COLOR: blue">true;<o:p></o:p>
Response.ExpiresAbsolute=DateTime.Now.AddDays(-1d);
Response.Expires =-1500;
Response.CacheControl = "no-cache";
 if(Session["SessionId"] == null)
    {
	Response.Redirect ("WdetLogin.aspx");
    }
}
try this
cool adch replied to theiry henry on 08-Jun-09 05:38 AM
Try adding this to the top of your page
Code:
Response.expiresabsolute=now() -1
or you could try

Code:
<meta http-equiv="PRAGMA" value="NO-CACHE">
<meta http-equiv="Expires" content="Mon, 01 Jan 2026 12:00:00 GMT">
or finally, one i found on the net but havnt tried is

Code:
Response.CacheControl = "no-store"
Response.AddHeader "Pragma", "no-cache" 
Response.Expires = -1
im sure Response.CacheControl = "no-cache" prevents cacheing on server but i may be wrong
try this link
cool adch replied to theiry henry on 08-Jun-09 05:39 AM

http://geekswithblogs.net/vivek/archive/2007/02/24/107148.aspx

http://www.daniweb.com/forums/thread35732.html#

http://www.4guysfromrolla.com/webtech/111500-1.shtml

thnks guys it was working.
theiry henry replied to Ravenet Rasaiyah on 08-Jun-09 05:54 AM
thnks guys it was working.
welcome buddy
Ravenet Rasaiyah replied to theiry henry on 08-Jun-09 05:56 AM
end of post
Sushant replied to cool adch on 06-Apr-10 04:50 AM
Hi Friend,
I had tried ur code but c#code is  not  working.


protected void Page_Init(object Sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
Response.Cache.SetNoStore();
}


pls. give me suggestion.

Thanks and Regards
-:Sushant :-