8

I have a WebForms application where the first page is basically a grid containing links to a second page which loads up a PDF viewer. The grid is actually located in a .ascx control. Everything is working going from the first page to the PDF viewer page. However, when I hit the back button to return to the first page. I get the following error (in Chrome, but also this is happening in other browsers):

Cache miss error

If I click the back button then the browser returns to the first page and everything is fine, but I need to resolve this error.

I have tried disabling the cache in the first page based on the recommendation from this StackOverflow answer, like so:

Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0.
Response.AppendHeader("Expires", "0"); // Proxies.

I have also tried this:

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
Response.Cache.SetExpires(DateTime.MinValue);

I've placed this code in the code behind of the .aspx page and in the .ascx control (in the OnInit methods), all to no avail. What am I missing here?

Community
  • 1
  • 1
FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
  • 2
    what is your code to navigate to the page with the pdf viewer? specifically are you using a post-back to do the navigation? – JJS May 20 '15 at 19:04

2 Answers2

4

As @JJS implied it sounds like you are using a OnClick (possible via LinkButton or ImageButton) which is causing a partial/full postback and your redirect is occurring in the code-behind. If this is correct, is there a reason why you are not using HyperLinks?

Cache handling is not the fix. The browser is stating a post occurred between pages.

Drytin
  • 366
  • 2
  • 8
  • I didn't have a good reason for not using HyperLink controls. I switched to those and now it works. Thanks for helping me remember the different purposes for LinkButtons and HyperLinks! – FishBasketGordo May 27 '15 at 18:04
0

Overide the OnPreInit method and set various caching-related properties on the Response object to prevent the browser from caching the page.

protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);

    Response.Buffer = true;
    Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
    Response.Expires = -1500;
    Response.CacheControl = "no-cache";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
}

you can do something similar to disable browser page caching, like the following:

   Response.Cache.SetCacheability(HttpCacheability.NoCache);
   Response.Cache.SetExpires(Now.AddSeconds(-1));
   Response.Cache.SetNoStore();
   Response.AppendHeader("Pragma", "no-cache");
Tharif
  • 13,794
  • 9
  • 55
  • 77