How to Refresh an Image in ASP.NET Automatically

by Updated June 27, 2011

You may need to have an image refreshed automatically on a web page in ASP.NET to get the latest image.  One instance where you might want the fresh image is if you upload an image that has the same file name as an already existing image file on the server, and when the page is Redirected or reloaded, you still see the old image, since it is cached. 

To get the latest image file in ASP.NET you can simply add a querystring to the end of the image file name that changes.  An easy way to do this is to just add the current DateTime to a querystring.  For example:

StoreLogoImage.ImageUrl = "~/someImage.jpg?refreshTime=" + Server.UrlEncode(DateTime.Now.TimeOfDay.ToString());

Because the file name with the TimeOfDay querystring will be different each time the page is refreshed, the latest image will be retrieved, instead of the cached image.

 


0
0

Add your comment

by Anonymous - Already have an account? Login now!
Your Name:

Comment:
Enter the text you see in the image below
What do you see?
Can't read the image? View a new one.
Your comment will appear after being approved.

Related Posts


Adding a CSS border to an ASP.NET Image control was a mystery to me for the longest time. While you could easily use an html image and add the runat="server" to it and then add CSS, I really wanted to use an asp:Image control along with a CSS border....  more »

So below I'm going to share with you a fairly easy to use and understand ASP.NET User Control that allows you to pick a Date (with the ajaxToolkit CalenderExtender) and also select the Time of day using a drop down list. I've named the control...  more »

This is one of those simple web page design things that can drive a web developer absolutely crazy.  more »

If you do any sort of ASP.NET programming there usually comes a time when you need to get a websites Base URL. The following shows two examples, the first example shows how to get the Base Site Url using C#, which can be used for getting both the...  more »

Here's how you can UrlEncode the plus sign (+) in a URL querystring in ASP.NET and then retrieve the plus symbol after UrlDecoding the string. In this example, I will do a postback and redirect the Server.UrlEncoded string to another page. First we will...  more »