Click here to Skip to main content
15,881,413 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.1K   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 
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.