Last updated: 27th March 2025
Browsers store images and other static assets in their cache, a temporary storage space on your computer to reduce loading time and save bandwidth. This caching mechanism allows websites to load faster by retrieving content directly from the cache. However, when website content, like images, changes frequently, it becomes necessary to clear the browser cache to display the updated content. In this guide, I’m sharing a simple yet effective method to dynamically clear the browser cache and ensure updated images are loaded using ASP.NET with C# and VB.NET.While updating my Easy Image Resizer tool, I encountered an issue where the browser would display the previously selected image instead of the new one whenever I selected an image for resizing or cropping. This behavior is actually the browser's default caching mechanism. To resolve this, I discovered a straightforward solution—a clever trick that ensures the updated image is displayed seamlessly.
💡 Browsers do not scan every image (or any content) to check if it’s the same as before. Browser cache works by identifying objects, such as images, by its name.
Using Asp.Net, I can easily resolve this by dynamically changing the image's name on the server side. By appending the current date and time (which are always unique) to the image name, I ensure that a new image name is sent to the browser each time, effectively bypassing the cache.
This is how you should do this.
The <img /> element below has no source. I'll assign an image dynamically using code behind procedure.
<img id="myImage" alt="image" src="" style="border:none;max-width:100%;" runat="server" />
protected void Page_Load(object sender, EventArgs e) { myImage.Src = ""; myImage.Src = "~/car.png" + "?" + DateTime.Now.Ticks.ToString(); // Add date time to prevent caching. }
Here's how it shows the image name in the browser console window.
The trick is, the code concatenates a new date and time with the image name for every page load, thereby receiving a new source name for the same image.
Note: If you typically store images in a database, this technique can be particularly effective. Simply append the current date and time to the image name before saving it. Since the date and time are always unique, this ensures each image has a distinct identifier.
myImage.Src = "" myImage.Src = "~/car.png" & "?" & DateTime.Now.Ticks.ToString ' Add date time to prevent caching.
This process is simple, and you can easily get the name of the image by simply replacing the string after the ? character.