ASPSnippets

Alerts
Get notified when a new article is published.

Name
 
Email

Your email will always be private and will not be shared.

Follow us on twitter.
 
Issue: Images with same name or URL do not get updated when file is changed in ASP.Net
Author Name: Mudassar Khan Published Date: September 30, 2009
Filed Under :
Issues and Exceptions
Views: 2194

Here I am explaining the issue when two images have same name and URL, thus even if the image is replaced the browser still displays the old image in spite of page reload and page refresh.

 

Cause

Since the images have same name and URL browser picks the image from the cache and displays the same for faster loading of the pages. Thus even if you change the image server side the same cached image is displayed until you clear the cache of the browser.

 

Solution

Now since it is not possible to clear cache of user’s browser from server side. We can use the following trick

C#

Image1.ImageUrl = "~/images/myImage.jpg?" + DateTime.Now.Ticks.ToString();

 

VB.Net

Image1.ImageUrl = "~/images/myImage.jpg?" + DateTime.Now.Ticks.ToString()

 

The idea is to append some data to the image URL so that the URL becomes unique. When the URL is unique the browser will always download the file from the server instead of loading it from cache

The following figure displays the page source which describes the output URL of the image highlighted




That’s it. Hope you liked it.


If you like this article, help us grow by bookmarking this page on any social bookmarking site.
Bookmark and Share Page copy protected against web site content infringement by Copyscape

Related Articles

Comments

mert erol said:
thank you for solution how we use this code pls can you give an example
May 11, 2010  

Mudassar Khan said:
Reply To: mert erol
You can use the same way as shown above. I have provided Vb and C# code
May 11, 2010  

Add Comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
There is no need to add BR tags. Simply press enter for new line

Name*  
Email*
Comment*  
Security code
Security code