Solved

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

Posted on 2011-03-19
13
1,170 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS ReportViewer report timeout 7 122
Run time Error 4 47
ASP.NET - Bind Gridview Datasource to a specific table in dataset 3 27
ASP.NET gridview select textbox on focus 2 33
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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

18 Experts available now in Live!

Get 1:1 Help Now