Tuesday, 13 March 2012

disable back button using Javascript or C# Code behind in asp.net


How to disable back button either using Javascript or C# Code behind in asp.net
 
Using Javascript (This will disable only back button not history)

<script>
  function testSp()
  {
   window.history.forward();

  }
  window.onload=testSp;
</script>

Using code behind:- (This will avoid store history in client side. So there will be no back button)

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