MJ_Developer
MJ_Developer

Reputation: 546

Problem with cache

I have problem with cache.

When I change the ImageUrl property of Image control, sometimes the browser uses of cache and therefore the new image isn't shown.

How can I solve the problem so that after changing the ImageUrl property, the new image to be shown?

Here is my code:

string url = some code;

imgAvatar.ImageUrl = url;

Upvotes: 0

Views: 292

Answers (2)

Jeffrey Blake
Jeffrey Blake

Reputation: 9699

Another way to avoid caching of individual components is something Joel discussed in an early StackOverflow podcast: attach an unique identifier as an http parameter on the end of the url for the component you want to avoid caching. Then update that identifier whenever you need to ensure users get the new version.

So if your URL is "http://www.mysite.com/images/myimage.png" then you might use "http://www.mysite.com/images/myimage.png?v=20110819a"

Note: Usually this trick is more applicable for Javascript and/or CSS than for images though.

Upvotes: 1

Ivo
Ivo

Reputation: 3436

You can turn off the page caching by adding the following options

      Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
        Response.Cache.SetNoStore();
        Response.Buffer = true;
        Response.CacheControl = "no-cache";
        Response.AddHeader("Pragma", "no-cache");
        Response.AppendHeader("Cache-Control", "no-store");
        Response.Expires = -1441;

Upvotes: 3

Related Questions