Preventing users to go back to the previous pages
By AzamSharp
Views: 3403

Preventing users to go back to the previous pages

Introduction:

Sometimes we are in a situation that we don't want the user to visit the previous pages. This can be a scenario when the user logs out but uses the back button to navigate to the pages. In this article I will show you some simple ways you can use to prevent user from going back.

Using History(+1) Method:

Let's say that you have a page called DemoPage.aspx which is the secure page and when you click the button you are redirected to another page FinalPage.aspx. Now from FinalPage.aspx you don't want to go back to the DemoPage.aspx. You can achieve this easily using few lines of code.

Let's first catch the button click event and send the user to the FinalPage.aspx page.

protected void Button1_Click(object sender, EventArgs e)

{

Response.Redirect("FinalPage.aspx");

}

Now if you press the button you will be redirected to the FinalPage.aspx page. If you press the back button you will be taken to the DemoPage.aspx page. Now we will implement the functionality that will prevent the user to go to the DemoPage.aspx page.

In the DemoPage.aspx html write the following code:

<body onLoad="if(history.length>0)history.go(+1)">

That's it. It means every time you will go to the DemoPage.aspx page you will be forwarded to the FinalPage.aspx.

Making a page with no toolbar:

Another solution is to make a page with no toolbar. No toolbar means that it will have no buttons, no navigation toolbar etc. In the Page_Load event of the DemoPage.aspx implement this code:

// Attach the event to the button click control

Button2.Attributes.Add("onclick", "CreateWindow()");

And now in the JavaScript code simply create the function CreateWindow which will open a new window and close the current window.

<script language =javascript>

function CreateWindow() {

window.open('SmallWindow.aspx','smallwindow','toolbar=false;target=_parent',true);

// just close the window and don't ask to close it

window.opener = self;

window.close();

}</script>

 

That's it. Now the new page SmallWindow.aspx will open without any toolbar hence you will not be able to go back.

I hope you liked the article, happy coding!

By AzamSharp


Enter Comment/Feedback
  •  
  •  
  •  
  •  
  •  

Comments/Feedbacks
Subject: Regarding getting the values of parent page
Name: Amar
Date: 2/1/2007 10:43:13 PM
Comment:
Hi Azam,
This is a fine article regarding using javascript on preventing the user to navigate to the previous page.
Please just send me a example worked in javascript which is used to get the values of controls in parent window from the click event in child window,I think this can be acheived by using window.parent but I don't know how exactly to use this.
Thanks
Subject: making a page with no tool bar
Name: azeem
Date: 5/9/2007 10:24:11 PM
Comment:
hello sir, this is azeem, i liked very much your article, but here instead of opening in a smallwindow can we open in a large window, if possible please tell me, i will be thankful to you.
Subject: RE: Opening large window
Name: AzamSharp
Date: 5/10/2007 11:24:41 AM
Comment:
Hi Azeem,

You can open the window of any size. All you need to do is to define the height and width properties in the window.open function.

Subject: dunno
Name: shpjolin
Date: 6/5/2007 8:53:35 PM
Comment:
good code!
Subject: question
Name: Nitha
Date: 4/11/2008 4:31:52 AM
Comment:
Good article.But,even if the toolbar is not there,we can still use the back button functionality.how do we prevent this?



Join WebHost4Life.com






Copyright GridViewGuy 2007-2008