Solved

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

Posted on 2011-03-19
13
1,187 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
ID: 35171328
You need to put nocache

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

Expert Comment

by:Roman Gherman
ID: 35171342
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
ID: 35171359
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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35171395
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
ID: 35171417
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
ID: 35186545
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
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35188335
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
ID: 35189676
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
ID: 35195100
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
ID: 35943923
Please cancel this request
0
 

Author Comment

by:TUS11
ID: 35945536
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
ID: 35945537
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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