Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-03-19
13
Medium Priority
?
1,359 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses

971 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