Click here to Skip to main content
12,446,418 members (56,354 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

15.6K views
12 bookmarked
Posted

Updating Images on Page

, 5 Aug 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

elliotmccardle
President VNetPro Software
United States United States
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionWhy abuse HTTP when you can use it properly? Pin
taxexile5-Aug-08 8:25
membertaxexile5-Aug-08 8:25 
AnswerRe: Why abuse HTTP when you can use it properly? Pin
elliotmccardle5-Aug-08 8:40
memberelliotmccardle5-Aug-08 8:40 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
taxexile5-Aug-08 23:38
membertaxexile5-Aug-08 23:38 
AnswerRe: Why abuse HTTP when you can use it properly? Pin
Huenemeca5-Aug-08 10:36
memberHuenemeca5-Aug-08 10:36 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
taxexile6-Aug-08 0:08
membertaxexile6-Aug-08 0:08 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
Huenemeca6-Aug-08 4:13
memberHuenemeca6-Aug-08 4:13 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
taxexile6-Aug-08 5:31
membertaxexile6-Aug-08 5:31 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
elliotmccardle6-Aug-08 4:35
memberelliotmccardle6-Aug-08 4:35 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
taxexile6-Aug-08 5:32
membertaxexile6-Aug-08 5:32 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
elliotmccardle6-Aug-08 6:03
memberelliotmccardle6-Aug-08 6:03 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
taxexile6-Aug-08 7:00
membertaxexile6-Aug-08 7:00 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
elliotmccardle6-Aug-08 7:12
memberelliotmccardle6-Aug-08 7:12 
GeneralRe: Why abuse HTTP when you can use it properly? [modified] Pin
taxexile6-Aug-08 8:25
membertaxexile6-Aug-08 8:25 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
elliotmccardle6-Aug-08 8:45
memberelliotmccardle6-Aug-08 8:45 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
taxexile6-Aug-08 13:12
membertaxexile6-Aug-08 13:12 
AnswerRe: Why abuse HTTP when you can use it properly? Pin
crashedapp5-Aug-08 12:22
membercrashedapp5-Aug-08 12:22 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
taxexile5-Aug-08 23:17
membertaxexile5-Aug-08 23:17 
AnswerRe: Why abuse HTTP when you can use it properly? Pin
spoodygoon5-Aug-08 15:48
memberspoodygoon5-Aug-08 15:48 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
taxexile5-Aug-08 23:16
membertaxexile5-Aug-08 23:16 
AnswerRe: Why abuse HTTP when you can use it properly? Pin
SimonMarsh5-Aug-08 23:42
memberSimonMarsh5-Aug-08 23:42 
GeneralRe: Why abuse HTTP when you can use it properly? Pin
taxexile6-Aug-08 0:08
membertaxexile6-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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160811.3 | Last Updated 5 Aug 2008
Article Copyright 2008 by elliotmccardle
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid