Click here to Skip to main content
15,890,043 members
Articles / Web Development / ASP.NET

Updating Images on Page

Rate me:
Please Sign up or sign in to vote.
2.75/5 (3 votes)
5 Aug 2008CPOL1 min read 23.2K   12   21
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:

VB
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)


Written By
President VNetPro Software
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhy abuse HTTP when you can use it properly? Pin
taxexile5-Aug-08 8:25
taxexile5-Aug-08 8:25 
AnswerRe: Why abuse HTTP when you can use it properly? Pin
elliotmccardle5-Aug-08 8:40
elliotmccardle5-Aug-08 8:40 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
taxexile5-Aug-08 23:38
taxexile5-Aug-08 23:38 
AnswerRe: Why abuse HTTP when you can use it properly? Pin
Huenemeca5-Aug-08 10:36
Huenemeca5-Aug-08 10:36 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
taxexile6-Aug-08 0:08
taxexile6-Aug-08 0:08 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
Huenemeca6-Aug-08 4:13
Huenemeca6-Aug-08 4:13 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
taxexile6-Aug-08 5:31
taxexile6-Aug-08 5:31 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
elliotmccardle6-Aug-08 4:35
elliotmccardle6-Aug-08 4:35 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
taxexile6-Aug-08 5:32
taxexile6-Aug-08 5:32 
I gave my credentials because they were brought into question by Huenemeca. Do keep up. I am not concerned about people criticising me; I'm used to it, however having extensive experience (which gave me the insight into NOT using this technique) and ratified qualifications in the subject does seem to alienate the more inept engineers amongst us. This place is becoming less technical by the day with mentions of "niceness" and also people not even reading threads! It's not *that* difficult! I suspect it will only be a matter of time before I am compared to a Nazi!


Getting back to the issue the correct behaviour is to set the appropriate http headers and not create random uris! Do use HTTP to control caching. Period. That's what the transport layer is there for! Do please read http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html[^]
GeneralRe: Why abuse HTTP when you can use it properly? Pin
elliotmccardle6-Aug-08 6:03
elliotmccardle6-Aug-08 6:03 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
taxexile6-Aug-08 7:00
taxexile6-Aug-08 7:00 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
elliotmccardle6-Aug-08 7:12
elliotmccardle6-Aug-08 7:12 
GeneralRe: Why abuse HTTP when you can use it properly? [modified] Pin
taxexile6-Aug-08 8:25
taxexile6-Aug-08 8:25 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
elliotmccardle6-Aug-08 8:45
elliotmccardle6-Aug-08 8:45 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
taxexile6-Aug-08 13:12
taxexile6-Aug-08 13:12 
AnswerRe: Why abuse HTTP when you can use it properly? Pin
crashedapp5-Aug-08 12:22
crashedapp5-Aug-08 12:22 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
taxexile5-Aug-08 23:17
taxexile5-Aug-08 23:17 
AnswerRe: Why abuse HTTP when you can use it properly? Pin
spoodygoon5-Aug-08 15:48
spoodygoon5-Aug-08 15:48 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
taxexile5-Aug-08 23:16
taxexile5-Aug-08 23:16 
AnswerRe: Why abuse HTTP when you can use it properly? Pin
SimonMarsh5-Aug-08 23:42
SimonMarsh5-Aug-08 23:42 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
taxexile6-Aug-08 0:08
taxexile6-Aug-08 0:08 

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

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