Tomasz Rabiński | zavaz Blog

C# SharePoint VSTO

Preventing browser from cachning ASPX or ASCX content

Caching content by the browsers can be sometimes very annoying. I had problems with it,  mostly when I was developing some popup modal dialogs which showed aspx or ascx controls.

Thankfully, there is very simple solution how to disable content caching. You achieve it in two ways:

1. Directly in the ASPX or ASCX using below code:


<% System.Web.HttpContext.Current.Response.AddHeader( "Cache-Control","no-cache");
System.Web.HttpContext.Current.Response.Expires = 0;
System.Web.HttpContext.Current.Response.Cache.SetNoStore();
System.Web.HttpContext.Current.Response.AddHeader("Pragma", "no-cache");%>

2. From the codebehind, for example in the OnLoad event:


protected override void OnLoad(EventArgs e)

{

System.Web.HttpContext.Current.Response.AddHeader( "Cache-Control","no-cache");
System.Web.HttpContext.Current.Response.Expires = 0;
System.Web.HttpContext.Current.Response.Cache.SetNoStore();
System.Web.HttpContext.Current.Response.AddHeader("Pragma", "no-cache");

}
Tag:
  • Rayonna says:

    Why do I bother clalnig up people when I can just read this!

    November 30, 2011 at 09:41

Your email address will not be published. Required fields are marked *

*


*