Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to disable back button in the menu bar so that a user does not go to the previous page

I have been searchiong net for a solution.

I have found many suggestions but none seems to work for me.

To prevent a user from goin to previous page< I have, on my aspx page
HTML
<head  runat="server">    
<title><title>
<script type="text/javascript">
        function DisableBackButton() 
        {
            window.history.forward()
        }
        DisableBackButton();
        window.onload = DisableBackButton;
        window.onpageshow = function (evt) { if (evt.persisted) DisableBackButton() }
        window.onunload = function () { void (0) }
 <script>
</head>
<body  önunload="disableBackButton()">
 //code for page from where i dont want users to use back button
</body>

In my aspx.cs page i have the code behind
C#
protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
        Response.Cache.SetNoStore();

        Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1d);
        Response.Expires = -1500;
        Response.CacheControl = "no-cache";
        Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);

    }

It has not worked. the back button still takes it to previous page.

I have also tried
C#
protected void Page_Init(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
        Response.Cache.SetNoStore();
    }

I have also tried with the above code
C#
protected override void OnPreRender(EventArgs e)
{
        string strDisAbleBackButton = "";
  ClientScript.RegisterClientScriptBlock(this.GetTyp(), "clientScript",strDisAbleBackButton);

}

But nothing seems to work

Finally i have written a warning message for users that they should not use the back button but users seem to ignore this message and get into problem because the two pages have different roles and role of user is dynamically cahnged when the user goes from first apge to second one and on use of back button the user is not accepted because of different role.

Kinldy help me

Many thanks for your help
Posted
Updated 23-Feb-14 4:50am
v3

You can also try this:
HTML
<META Http-Equiv="Cache-Control" Content="no-cache"/>
<META Http-Equiv="Pragma" Content="no-cache"/>
<META Http-Equiv="Expires" Content="0"/>

Source: Browser back button issue after logout[^]
 
Share this answer
 
Comments
Member 10235977 24-Feb-14 1:57am    
It is not clear on which page this code is to be placed, because use can logout from any page. I tried plaing this code on login page to which the user is directed on logout but it does not seem to work there, as it prevents back button on one clcik but permits on second click. I have used the other options given on the reference. These also have the same result. I cant imagine placing on each and every page, as there may be hundreds of pages. It did not work for me on the login page.
Thomas Daniels 24-Feb-14 12:36pm    
You can try to put it on each page. You don't have to add it all manually, but you can use Master Pages for this purpose.
Window Forward[^]
Manipulating Browser History[^]
Disabling browser back button, you should not or say you cannot. Check the links I have provided. These just maipulate with the browsers history letting you feel the Back button is disabled but not exactly.
Window.History.Forward() is a simple one. Which prevents the user going back to previous page. The second link helps you find more methods to manipulate on this.
Hope this helps.
Post back your queries if any.
Thanks.
:)
 
Share this answer
 
 
Share this answer
 
Comments
Member 10235977 24-Feb-14 1:31am    
Thanks. I was using the same code but i was using on second page whereas it has to be used on the first page to whcih you do not want the users to go back. Many thanks
Following has been much helpful and works like a charm for me.

Following was posted by YogeshwarBK108 HERE

Quote:
If a page should not be displayed when back button is pressed after logout, that page should contain this code.

protected void Page_Load(object sender, EventArgs e)
{

this.Response.Cache.SetExpires(DateTime.UtcNow.AddYears(-4));
this.Response.Cache.SetValidUntilExpires(false);
this.Response.Cache.SetCacheability(HttpCacheability.NoCache);
this.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
this.Response.Cache.SetNoStore();
this.Response.ExpiresAbsolute = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0));
this.Response.Expires = 0;
this.Response.CacheControl = "no-cache";
this.Response.AppendHeader("Pragma", "no-cache");
this.Response.Cache.AppendCacheExtension("must-revalidate, proxy-revalidate, post-check=0, pre-check=0");


if(Session["SessionId"] == null)
{
Response.Redirect ("login.aspx");
}

}
protected void BtnLogout_Click(object sender, EventArgs e)
{
Session.Abandon();
Response.Redirect("login.aspx");
}


In my Application, we have a page controller class from which every page is inherited and session handling was done there. so i placed the code mentioned in above page load there to handle browser back button functionality after logout.


I hope it will help you. :)
 
Share this answer
 
Comments
Member 10235977 2-Mar-14 5:26am    
How do i use page controler class. Any tutorial or reference that can help shall be useful. Thanks
VICK 2-Mar-14 23:55pm    
You can create a class named PageController and than you can inherit your pages from that class like below,

public partial class Yourclass : PageController

and you can do your session handling inside this class.
JavaScript
 <script type="text/javascript" language="javascript">
        function DisableBackButton() {
             window.history.forward()
         }
         DisableBackButton();
         window.onload = DisableBackButton;
         window.onpageshow = function (evt) { if (evt.persisted) DisableBackButton() }
         window.onunload = function () { void (0) }


       //  OnClientClick = "DisableBackButton()"
</script>
 
Share this answer
 
<head>
XML
<script type="text/javascript">
    window.history.forward();
    function noBack() { window.history.forward(); }
</script>
</head>
<body  önload="noBack();">
 
Share this answer
 
v2
C#
protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
        Response.Cache.SetNoStore();

        Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1d);
        Response.Expires = -1500;
        Response.CacheControl = "no-cache";
        Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);

    }
Write This Code at Master Page when you redirect after Login.(Home Page After Login Or Master Page of Home Page.)

-----------------------------------------------------
Also you can write this(below) code at login page Onload event.

<script type="text/javascript">
        function preventBack() { window.history.forward(1); }
        setTimeout("preventBack()", 0);
        window.onunload = function () { null };
    </script>
 
Share this answer
 
v2
 
Share this answer
 
v2
Comments
CHill60 11-Feb-15 11:25am    
You're a year late with this and haven't added anything substantial to the previous solutions
Google "disable back button" and you'll see this question has been asked (every day) for over 15 years. Has it not occurred to you that the reason you haven't found an answer is simply because it can't be done? Just because you want to do something...even want it really bad, doesn't mean it can be done.

Instead of trying to do something that can't be done, tell us what unwanted behavior, or what bug etc you are seeing when people go back. You then have to work on that as an issue as you're never going to disable the back button so if you put all your eggs in that basket you will never solve your problem.
 
Share this answer
 
Comments
CHill60 11-Feb-15 11:23am    
Alternatively OP could use solutions 1, 3 or 5 posted a year ago
F-ES Sitecore 11-Feb-15 11:49am    
None of which disable the back button as the OP originally requested.
CHill60 11-Feb-15 12:00pm    
Very true. And I agree it can't be done. But the year-old solutions solve the problem that the OP didn't express particularly well, i.e. preventing a user going back to a previous page which is now invalid... "role of user is dynamically cahnged when the user goes from first apge to second one and on use of back button the user is not accepted because of different role."
F-ES Sitecore 11-Feb-15 12:25pm    
Which is exactly what I said to do. Glad my advice worked

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900