This example explains how to Disable Browser Back Button Using Javascript In ASP.NET. 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
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
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 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
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
Response.Cache.SetNoStore();
}
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
Disable Browser Back Button Using Javascript ASP.NET
Posted by
Unknown
<script type = "text/javascript" >
<%@ Page Language="C#" AutoEventWireup="true"
If you are using firefox then use <body onunload="disableBackButton()"> instead of onload
protected void Page_Init(object Sender, EventArgs e)
Doing this,user will get the page has expired message when hitting back button of browserIf you like this post than join us or share
Labels: ASP.NET, JavaScript
Subscribe to:
Post Comments (Atom)
55 comments:
This comment has been removed by the author.
Worked for me!!! Thanks.
Not working ...I am still able to browse back..
@happy:
Which browser your are using ?
Please paste ur code here so that i can look into
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...
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
@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 ?
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
@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();
}
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
Is there any way I can trap Browser BACK button (IE6 is my browser)) and do something else on it?
Thanks,
Sumeet
wat about vb.net....if i hav to do same work wid it....plz help me out!
This comment has been removed by a blog administrator.
This comment has been removed by a blog administrator.
@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 ?
Thanks :)
how can i disable the refresh button using c#.net?
@Above:
For detecting page refresh read my article mentioned below
Detect Page refresh in ASP.NET
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
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...
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...
How do I do disable the back button in classic ASP? I am a beginner in classic ASP. New to .NET also.
I want the back and forward button in disabled states only like when we open the browser for first time.
can we?
Thanks it works....
Hi Jay Hr Thnx a lot buddy it did work.My email jayvengs@gmail.com
Thanks for this article
Amit,
What if the page you are trying to disable the back button for is just a content place holder on a master page, and the body tag is located in the master page. I don't want to disable the back button on my entire site, just when the user submits a specific request.
Hii.. I m Pooja..
Your code will not work..
My can't disable my back button using javascript code..
plz help me to desable back button
as early as possible..
Thanks it helped me. Appreciation to the developers.
Good Job,and thank all of you that post code like this~~~~
its working.. thank you so much..
see this http://sintaks.blogspot.com/2010/08/javascript-disable-functionality-of.html
It works great. But i want to know how to do it in php because i am developing an application where i need this point.
browsers rendering
code works..thanks.
I disable the back button when user click on logout and it works.but when user click on other links after login then also he is unable to go back.
thnks in advance
The following post describes a few more methods to disable browser back button..
http://www.robinthomas.in/dotnet/restrict-user-go-back-to-previous-page-after-signout/
tthhanxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
this doesnot work in IE 7.
Thanks a lot.I wanted it for my project
thanks it worked!!!
Thank you very much. It helped me a lot. Thank you once again.
hi,
it is working only with IE...
Thanks Amit.
hi iam subbarao...
In my web application when i clicked logout button its redirecting to login page but when i clicked on back button in browser its going back to the pages which should appear after login.I should Prevent it according to my application.
my log out button code is
Button_click()
{
Session.abondon();
Response.Redirect("~LoginPage.aspx");
}
please help me ...
how to use this in blogspot?
my blog is
http://howtohackthe.blogspot.com
Thanks the javascript solution worked and fixed my issue in all pages of the site.
This is idea works well in google but failed in Internet Explorer 9, Firefox 9 etc
In my web application when i clicked logout button its redirecting to login page but when i clicked on back button in browser its going back to the pages which should appear after login.I should Prevent it according to my application.
my log out button code is
Button_click()
{
Session.abondon();
Response.Redirect("~LoginPage.aspx");
}
please help me ...
and send me java script
@Abhishek: Configure and use Login control with asp.net membership provider in your application to deny anonymous access to your website.
Read Login Page Using Login Control Example In Asp.Net to know how to configure asp.net membership provider and login control
Hi..I have a master page for my application. Other pages are ajax implemented. somehow this code is not working for me i have placed this code in the master page..so that it will apply to all the other pages is that right ?...could you please let me if there is a workaround...
thanks a lot . the code worked for me . i am using chrome browser..
hi i am using your code at the page load event of my login page....
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
Response.Cache.SetNoStore();
}
on the click of log out button code i have added the code :-
protected void Log_Out_Click(object sender, EventArgs e)
{
Session.Abandon();
Response.Redirect("~/Log_In.aspx");
}
but....after being re-directed to the login page.....back button is still working....
pls help....!!!
thenx fo all your efforts!!! :)
Its Working!!!!!!!
Thank You Very Much!
Hi sir,
I want to use this functionality in html pages,
i am using onbeforeunload() function on html page.
Its not working with this function.
Any suggestion?
Thanks I have used the code to prevent the user to go to back to previous page, but when I used master page and placed the code in that master page I am unable to navigate among pages that are in herited from the master page. How can I solve this with this problem Please do specify answer
Thanks
Ganesh
Thanks its working,,,,,
Post a Comment