C# .NET - disable BACK button after login - Asked By Ratnesh Soni on 01-Aug-09 10:43 AM

hello sir,

            i want to disable back button after i made login.bcoz when user press back it not goes to login page it remains on home page.

tell me script for this.

or tell me another by which can't go back after login it goes when he press logout button      

Re - Ravenet Rasaiyah replied to Ratnesh Soni on 01-Aug-09 11:31 AM

HI

There is two way

1. client side

Just add this scrip in your after logged in page.


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication3._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></title>

<script language="JavaScript">

javascript: window.history.forward(1);

</script>


</head>
<body>
<form id="form1" runat="server">

<div>

</div>

</form>
</body>
</html>

2.Server side

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace WebApplication3
{

public partial class _Default : System.Web.UI.Page
{


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

}


}
}


Thank you
http://www.codegain.com

re - Web Star replied to Ratnesh Soni on 01-Aug-09 04:31 PM

its' easy when you loggened  and come to your home page, there write java script liek this

<script type=javascript>
histiory.Load(0)
</script>
or else like this way here great sample

http://www.codeasp.net/blogs/vivek_iit/microsoft.net/19/-disable-back-browser-button-aspnet

or

Disabling the Browser's buttons do not makes any sense.

If you open a new browser window on click event, you can hide the buttons but the user can still navigate with the keyboard. If you can able to disable the keyboard keys for Back and Forward Navigation, then still its not sure that it will work with every browser.

So Better to handle this problem by Coding in Vb or C#.A better approach is to disable caching so  the page must be requested
fresh each time from Server.

Use this code on Page_Load event of the page.
Response.Expires = 0
Response.Cache.SetNoStore()
Response.AppendHeader("Pragma", "no-cache")

So whenever the Page_Load event occures, it will ask a new copy from server, so that we can write code which will check that wheather the request is valid or not. This Condition is depend on your application requirment.

To Disable Back and Forward Navigation of Browser follwing is the Script

<head>
<script language="JavaScript" type="text/javascript">
           javascript:window.history.forward(1);
</script>
</head>

But i should not recommand you to use this JavaScript, Because if the JavaScript is Disable in the Browser, then your code will not work