Saturday, April 4, 2009

Disable Browser Back Button Using Javascript ASP.NET


javascript to disable browser back button

In this example i'm explaining how to disable browser's back button to avoid user going to previous page by clicking on back button of browser, for this we need to use javascript to prevent user navigating to previous page by hitting back button.

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 are using firefox then use <body onunload="disableBackButton()"> instead of onload

If you want to disable back button using code behind of aspx page,than you need to write below mentioned code

C# code behind
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
string strDisAbleBackButton;
strDisAbleBackButton = "";
ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "clientScript", strDisAbleBackButton);
} 

VB.NET code behind
Protected Overloads Overrides Sub OnPreRender(ByVal e As EventArgs)
    MyBase.OnPreRender(e)
    Dim strDisAbleBackButton As String
    strDisAbleBackButton = ""
    ClientScript.RegisterClientScriptBlock(Me.Page.[GetType](), "clientScript", strDisAbleBackButton)
End Sub


We can also achieve this by disabling browser caching or cache by writing this line of code either in Page_load event or in Page_Init event

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


Have fun

Download the sample code attached


Other articles on javascript:
1. Highlight gridview row on mouse over using javascript in asp.net and C# c-sharp

2. Disable copy paste cut and right click in textbox on aspx page using javascript

3 .Mozilla firefox JavaScript window.close() not working / does not work in firefox
www.tips-fb.com
Shout it
Stumble Upon Toolbar
Submit this story to DotNetKicks vote it on WebDevVote.com add to del.icio.us saved by 0 users
Subscribe to Feeds

23 comments:

mohsin said...

Hello
A good post with wonderful ideas

www.supirscience.blogspot.com

www.mnewsalert.blogspot.com


Arifur said...

This post has been removed by the author.


Anonymous said...

Worked for me!!! Thanks.


happy said...

Not working ...I am still able to browse back..


amiT
amiT jaiN said...

@happy:

Which browser your are using ?

Please paste ur code here so that i can look into


Anonymous said...

hi :) thanks for this post. I want to disable the browser catching through c# code and have tried what you have suggested but it is not working... the javascript code you have suggested is working fine but i want to disable browser catching. I am using IE7. The tool i am using is visual studio 2005, asp.net 2.0... please help me...


Anonymous said...

Hi Amit,

I have tried both of your solutions however none of them is working for me. I am able to use 'Back' button while i m on the second page.

What I am doing is, I created a simple HTML/JS page with one hyperlink to another dummy page with just text message in it. Now, while I am on the second page, my browser does "Allow" me to move back by clicking on the 'Back' button.

I tried C# version by pasting the C# code in page load as well as init of second page, but no success.

Can you help me?

Thanks,
Sumeet


amiT
amiT jaiN said...

@sumeet:

Which browser u r using ? if you are using firefox than u need to write onunload instead of onload

Also check whether javascript is enabled in your browser or not ?

I've checked the code again and it's working fine for me in IE or Firefox

Check this sample code

And do let me know whether it works for you or not ?


Anonymous said...

Amit, Thanks it worked last time after writing to you.

I am using IE6 JS is enalbed, can you tell me how can I make a page 'Do not store in cache' using C#? I am also using .Net 3.5 Framework


amiT
amiT jaiN said...

@Above:

Code mentioned below is used for disabling browser cache in ASP.NET


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


Anonymous said...

Do you know any way by which I can trap the Back button when it is clicked so that I can show an alert message to the user.

Also by using above code I have noticed that although the page does not reside in Temporary folder however, is there any possibility that I can also deny it from storing in the "Drop down" of BACK button??

Please suggest.


Thanks,
Sumeet


Anonymous said...

Is there any way I can trap Browser BACK button (IE6 is my browser)) and do something else on it?

Thanks,
Sumeet


prairana said...

wat about vb.net....if i hav to do same work wid it....plz help me out!


prairana said...

This post has been removed by a blog administrator.


Anonymous said...

This post has been removed by a blog administrator.


amiT
amiT jaiN said...

@prairana:

I've updated the code in article on how to disable back button using C# or VB.NET code behind.
Read it again and let me know if it solves ur query ?


Anonymous said...

Thanks :)


Anonymous said...

how can i disable the refresh button using c#.net?


amiT
amiT jaiN said...

@Above:

For detecting page refresh read my article mentioned below

Detect Page refresh in ASP.NET


Anonymous said...

Hi amit, I was used ur code ...But i need ur help... i used ur code behind in C# after session expire on LogOut button click event.. like that
logOut_click....
{
Session["UserName"]="";

used ur code behind code in C#

ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "clientScript", strDisAbleBackButton);

}


But i had a error on ClientScript

How can solve this problem


sam said...

Hi amit, I used ur C# code behind code in log out button click event....

but i got error in line no 8. i.e ClientScript

How i can solve this error.. I used internet explorer 6.0 Pls help me...


sagar said...

Hi amit, I used ur C# code behind code in log out button click event....

but i got error in line no 8. i.e ClientScript

How i can solve this error.. I used internet explorer 6.0 Pls help me...


Panduranga said...

How do I do disable the back button in classic ASP? I am a beginner in classic ASP. New to .NET also.


About Me

My Photo
amiT jaiN
Hi, I am amiT jaiN Software engineer working on C#.NET and ASP.NET technologies
View my complete profile

Comments

.NET Resources

Find More Articles


Subscribe To Feeds

Subscribe by E-mail

Enter your email address:

Delivered by FeedBurner


Subscribe in your favorite reader

This site is best viewed with || You may get errors in proper display of this site if using Internet explorer


C#.NET Articles and tutorials,ASP.NET Articles - blog by amiT jaiN