Click here to Skip to main content
Licence CPOL
First Posted 5 Aug 2008
Views 11,177
Bookmarked 12 times

Updating Images on Page

By | 5 Aug 2008 | Article
This article will show you how to update an image control on a webpage, avoiding caching issues.

Introduction

This article will explain how to avoid caching issues when changing an image on an ASP.NET webpage.

Background

We had a page that allowed a user to upload a new picture for their bio. The current image was displayed on this page as well inside a standard <asp:Image /> control. The page also contained a <asp:FileUpload /> control for the user to update the image. The uploaded image would keep the same name as the previous image and simply overwrite the existing file.

Our problem was that when the user would update the image, the old image was still being displayed until either the user closed the browser or browsed a separate site altogether, thus clearing the cached version.

Using the code

The way to workaround this is really quite simple. All you really have to do is "trick" the browser into thinking that it is showing a new image. Below is how you accomplish this:

Image1.ImageUrl = "mypicture.jpg?s=" & DateTime.Now.Ticks.ToString()

All that is necessary is to add some unique value after the image name (i.e., query string). I chose Ticks because it is mostly going to be unique. By doing this, you force the browser to retrieve the latest version of the image.

This is not a life altering posting, but I hope this helps someone.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

elliotmccardle

President
VNetPro Software
United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWhy abuse HTTP when you can use it properly? Pinmembertaxexile8:25 5 Aug '08  
AnswerRe: Why abuse HTTP when you can use it properly? Pinmemberelliotmccardle8:40 5 Aug '08  
GeneralRe: Why abuse HTTP when you can use it properly? Pinmembertaxexile23:38 5 Aug '08  
AnswerRe: Why abuse HTTP when you can use it properly? PinmemberHuenemeca10:36 5 Aug '08  
GeneralRe: Why abuse HTTP when you can use it properly? Pinmembertaxexile0:08 6 Aug '08  
GeneralRe: Why abuse HTTP when you can use it properly? PinmemberHuenemeca4:13 6 Aug '08  
GeneralRe: Why abuse HTTP when you can use it properly? Pinmembertaxexile5:31 6 Aug '08  
GeneralRe: Why abuse HTTP when you can use it properly? Pinmemberelliotmccardle4:35 6 Aug '08  
GeneralRe: Why abuse HTTP when you can use it properly? Pinmembertaxexile5:32 6 Aug '08  
GeneralRe: Why abuse HTTP when you can use it properly? Pinmemberelliotmccardle6:03 6 Aug '08  
GeneralRe: Why abuse HTTP when you can use it properly? Pinmembertaxexile7:00 6 Aug '08  
GeneralRe: Why abuse HTTP when you can use it properly? Pinmemberelliotmccardle7:12 6 Aug '08  
GeneralRe: Why abuse HTTP when you can use it properly? [modified] Pinmembertaxexile8:25 6 Aug '08  
GeneralRe: Why abuse HTTP when you can use it properly? Pinmemberelliotmccardle8:45 6 Aug '08  
GeneralRe: Why abuse HTTP when you can use it properly? Pinmembertaxexile13:12 6 Aug '08  
AnswerRe: Why abuse HTTP when you can use it properly? Pinmembercrashedapp12:22 5 Aug '08  
GeneralRe: Why abuse HTTP when you can use it properly? Pinmembertaxexile23:17 5 Aug '08  
AnswerRe: Why abuse HTTP when you can use it properly? Pinmemberspoodygoon15:48 5 Aug '08  
GeneralRe: Why abuse HTTP when you can use it properly? Pinmembertaxexile23:16 5 Aug '08  
AnswerRe: Why abuse HTTP when you can use it properly? PinmemberSimonMarsh23:42 5 Aug '08  
GeneralRe: Why abuse HTTP when you can use it properly? Pinmembertaxexile0:08 6 Aug '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 5 Aug 2008
Article Copyright 2008 by elliotmccardle
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid