Go to DotNetFunda.com
 Online : 53 |  Welcome, Guest!   Login
 Skip Navigation Links Home > Articles > ASP.NET > Handle back button of the browser
  Win at least 8 gifts every month now! >> Top Performers of this month.   


Notice: If page content has been copied from other sources without proper permission, kindly let us know with details from where it has been copied for further action.

All Articles | Submit Article |

Handle back button of the browser

 Posted on: 11/4/2009 5:04:33 AM by Virendradugar | Views: 582 | Category: ASP.NET | Level: Beginner | Print Article
This article explains various techniques to disable / Handle back button of the browser.

Webmaster says - If this article content has been copied from other websites without proper permission, kindly let us know with details from where it has been copied for further action.

Introduction
How many time, it has happened with you that when your client has asked you to that end-user should not be allowed to go back to previous page using browsers back button? But the actual problem is we don't have any control on the browser's back button. The “Back” browser button cannot be actually disabled by a web application as the browser security will not allow this. But yes there are number of workarounds using which we can achieve this functionality.

Solutions

Using JavaScript, we can forward the user to the same page if user presses the back button of the browser. See the code below :

 <script type="text/javascript" language="javascript">
    
    function GoBack()
    {
        window.history.forward();
    }
    </script>
Call this JavaScript function on the onload event of body tag.

<body onload="GoBack();">

This method will always redirect to the user on the current page itself.

For example, user is on page 1 and there is a link on Page 1 which takes user to Page 2. Now when user clicks on the link, browser redirects the control to Page 2. Now from page 2 if user press back button of the browser then user will be redirect to the page 2 itself.

window.history contains the collection of the page visited by the user for particular browser session. As an alternative of window.history.forward(), one can also use window.history.go(+1). As both are same.

But if we want to display web page expired warning on the click of back button as we normally see in all banking sites? Actually what happens is, when you press the back button, browser takes the page from cache and display it on screen. So if we want to show web page expired warning, we should not allow the browser to cache the page. We can achieve this via ASP.NET.

protected void Page_Load(object sender, EventArgs e)
{  
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
}

This code will not allow page to be cached. But one problem with this approach is that this will only work when there are some dynamic content in the page. i.e. change any drop down box value and then try to press back button.

Conclusion
Your comments are always appreciated. If you can find out better solution then this please let me also know.

Enjoy...


Interesting?   Share and Bookmark this kick it on DotNetKicks.com


Experience:3 year(s)
Home page:http://virendrakdugar.googlepages.com
Member since:Tuesday, August 11, 2009
Biography:Virendra Dugar is having 3+ years of experience in software development. He has very good command on ASP.NET, C#, Ajax and Javascript. He is currently working as a Software Engineer with a CMM Level 5 Company.

Visit his blog : http://virendradugar.wordpress.com
 Latest post(s) from Virendradugar

   ◘ Copy DLL from GAC posted on 11/14/2009 4:05:08 AM
   ◘ Handle back button of the browser posted on 11/4/2009 5:04:33 AM
   ◘ New Features of ASP.NET 4.0 posted on 10/12/2009 9:19:10 AM
   ◘ AsyncFileUpload Control - New Control in Ajax Control ToolKit posted on 10/5/2009 4:36:27 AM
   ◘ Using C# and VB.NET classes together in the App_Code folder posted on 9/29/2009 9:31:29 PM


Submit Article

About Us | The Team | Contact Us | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)