Solved

how to expired previous page when we click on back button in asp.net

Posted on 2011-03-19
13
1,128 Views
Last Modified: 2012-05-11
how to expired previous page when we click on back button in asp.net
0
Comment
Question by:TUS11
  • 5
  • 4
  • 3
13 Comments
 
LVL 9

Expert Comment

by:Roman Gherman
Comment Utility
You need to put nocache

------------------------
You can find me on: http://extremedev.blogspot.com
0
 
LVL 9

Expert Comment

by:Roman Gherman
Comment Utility
uuups,

Forgot to add the code:
<% System.Web.HttpContext.Current.Response.AddHeader( "Cache-Control","no-cache")
System.Web.HttpContext.Current.Response.Expires = 0
System.Web.HttpContext.Current.Response.Cache.SetN oStore()
System.Web.HttpContext.Current.Response.AddHeader( "Pragma", "no-cache")%>

Open in new window


Put that on the top of the aspx page or on code behind.
This will make the page not to be cached and when clicking back button in browser - it will not have anything to display except Web page expired.

------------------------
You can find me on: http://extremedev.blogspot.com
0
 

Author Comment

by:TUS11
Comment Utility
Hi,
  i add the above code on page load of the first page and when i redirect to second page from the first page then if i use the back button  on second page then it does not expired page message. it show the first page.


Please help me bcoz it is urgent for me.


Thanks
0
 
LVL 33

Expert Comment

by:Todd Gerbert
Comment Utility
Setting these seems to work in a quick test for me:

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();

Open in new window


Keep in mind, though, that ultimately it's up to the web browser and there is no guarantee that the browser will honor the cache control headers.
0
 
LVL 9

Expert Comment

by:Roman Gherman
Comment Utility
OK,

It looks like this only works if the connection is secure: https.

Otherwise you will have to use session variables and when navigate away then delete the variable from the session.

In codebehind check if page had "expired" - using the Session Variable then Redirect to a Web Page Expired page (a simple html page which you will have to create).

You will also set this:
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();

Open in new window

In order for page to make a postback when you click back button, so that you will be able to do your check on codebehind.

------------------------
You can find me on: http://extremedev.blogspot.com
0
 

Author Comment

by:TUS11
Comment Utility
Hi,

All above solution not working for me.
we are using http not https then in this case what i do for expiring the page.
Please help me.

Thanks
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 33

Expert Comment

by:Todd Gerbert
Comment Utility
What browser are you using, which version of the .Net Framework, what code in your ASPX page, and how are you determining that the page is still being cached?
0
 

Author Comment

by:TUS11
Comment Utility
hi,
    I am using IE8, 2.0 .net framework.
    and in code behind of ASPX page i am using this below code on page load.
System.Web.HttpContext.Current.Response.AddHeader( "Cache-Control","no-cache")
System.Web.HttpContext.Current.Response.Expires = 0
System.Web.HttpContext.Current.Response.Cache.SetN oStore()
System.Web.HttpContext.Current.Response.AddHeader( "Pragma", "no-cache")

For checking the page expiry i have the two page.
On first page i have a button which id redirected to the second page but i want that when we press BACK button then first page should be show the Page Expiry warning message.
and the above code i add on the first page .


Thanks
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
Comment Utility
You will only get the warning message you're describing if the expired page was retrieved as a result of a POST operation, not a GET.

e.g. Default.aspx has a button with it's PostBackUrl set to Default2.aspx, the user clicks the button (this causes the ASP.Net form to POST to Default2.aspx).  Default2.aspx sets no-cache; user navigates from Default2.aspx to Default3.aspx.  User clicks back button, content expired message is displayed.

However, if Default.aspx has a normal <a href="Default2.aspx">Link</a> link to Default2.aspx, when the user clicks it a GET will be used to retrieve Default2.aspx.  If the user naviages to Default3.aspx and then uses the back button, no warning message will be seen (because there was no POST data to have expired).

Default1.aspx:
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
		Click this link to GET Default2.aspx: <a href="Default2.aspx">Get Default2.aspx</a><br />
		<br />
		Click this button to POST form results to Default2.aspx:
		<asp:Button ID="SubmitButton" runat="server" Text="POST to Default2" PostBackUrl="~/Default2.aspx" />
    </div>
    </form>
</body>
</html>

Open in new window



Default2.aspx
<%@ Page Language="C#" %>
<%@ PreviousPageType VirtualPath="~/Default.aspx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
	private void Page_Load(object sender, EventArgs e)
	{
		Response.Cache.SetCacheability(HttpCacheability.NoCache);
		Response.Cache.SetNoStore();
	}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <a href="Default3.aspx">test</a>
    </div>
    </form>
</body>
</html>

Open in new window



Default3.aspx
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

Open in new window

tgerbert-435717.flv
0
 

Author Comment

by:TUS11
Comment Utility
Please cancel this request
0
 

Author Comment

by:TUS11
Comment Utility
I've requested that this question be closed as follows:

Accepted answer: 0 points for TUS11's comment http:/Q_26897839.html#35189676

for the following reason:

OK solution
0
 
LVL 33

Expert Comment

by:Todd Gerbert
Comment Utility
I think http:Q_26897839.html#a35195100 adequately demonstrates how to force a page to expire, complete with code and a screencast.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now