Loading ...

'Disable' Back Browser Button ASP.NET

 /5
0 (0votes)

The "Back" browser button (or for that matter any other browser button) cannot be actually disabled by a web application as the security context will not allow this (think of what nasty things could happen if web applications can remove buttons from client browsers!)

What we can do is to somehow make sure that browser does not cache the web pages (which will cause the"Back" "Forward" buttons to grey out), and this can be easily done by expiring the Response. The code for this has slightly changed in ASP.NET 2.0 (there is a new HttpCachePolicy class) :

   /*
    * Code disables caching by browser. Hence hitting the back browser button
    * causes the Page_Load event to fire again.
    */
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now); //or a date much earlier than current time

In ASP.NET 1.x the code was:

   Response.CacheControl = "no-cache";
   Response.AddHeader("Pragma","no-cache");
   Response.Expires = -1;

This will make the browser go to the server everytime the back button is clicked (for that particular page) and prevent any caching of the pages.

Comments (7)

   
Paul
I tried this on ASP .NET 2.0 and it does not seem to work - where are you putting that code to get it to work?
4/10/2009
 · 
by
   
Faheem
You have to put this
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now);
in Page_Load of final page from which u don't want to go back its working
8/28/2009
 · 
by
   
Kev
Kev
Code doesn't appear to work.
10/28/2009
 · 
by
   
lokesh
lokesh
this code doesn't work at all..
i've an appl in which when i logout i should not be able to go bak to previous page
my session should end there.
dont tell abt session.abandon() method
this also doesn't work
12/28/2009
 · 
by
   
Samar
Samar
Hello I m putting this following code in a final page Load(the form I don't want to go back) but it is not working

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now);

I M using .net 2.0 framework
1/25/2010
 · 
by
   
hassaanz
try this in ur logout button/link..its works for me...as it refreshs the page after the Session.abondon(); so its not possible to go bak to ur previous page without logging again.. protected void logOut_b_Click(object sender, ImageClickEventArgs e)
    {
        Session.Abandon();
        Response.AddHeader("Refresh",Convert.ToString((Session.Timeout * 0)));
        if(Session["Name"].ToString()=="")
        {
           Server.Transfer("login.aspx");
        }
    }   
7/4/2010
 · 
by
   
hassaanz
try this in ur logout button/link..its works for me...as it refreshs the page after the Session.abondon(); so its not possible to go bak to ur previous page without logging again.. protected void logOut_b_Click(object sender, ImageClickEventArgs e)
    {
        Session.Abandon();
        Response.AddHeader("Refresh",Convert.ToString((Session.Timeout * 0)));
        if(Session["Name"].ToString()=="")
        {
           Server.Transfer("login.aspx");
        }
    }   
7/4/2010
 · 
by

Top Posts