Click here to Skip to main content
Click here to Skip to main content

How to Solve Image Refresh problem associated with Image control in ASP.NET

, 7 Mar 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
How to Solve Image Refresh problem associated with Image control in ASP.NET

Introduction

This is a small tip on how to solve the Image refresh problem of Image control in ASP.NET

Background

Mamy a times, I have observed that

  1. (PREREQUISITE) I have an Image control on my page
  2. (STEP)I am uploading an image to be displayed on that control using Fileupload control.
  3. (STEP)I upload an image and it get displayed in Image control.
  4. (STEP)If I upload another image (no post-back in between and after a very short file) 
  5. (PROBLEM) many times the new image file get uploaded on the server but the image control could not show the new Image.

Using the code 

The problem seems to occur when I am saving the uploaded image in some
predefined file name. So first time the Image control recognized that
the file is new. the next time since the file-name is same but actually
the image is different the Image control chooses not to refresh itself
as it keep thinking that the same file is getting attached to it again.
(Perhaps this is some optimization mechanism inside).

To avoid such problem in the scenario, i tried to cheat the image control every time I upload a new image file. I attach a dummy query string parameter with the image URL which makes the Image control reconsider and shows the new Image instead.

Since this is one scenario I found this problem there could be other scenarios too so I made a habit of using the image control in following manner.

imgphoto.ImageUrl = path + "?time="+ DateTime.Now.ToString(); 

This ensures that the Image control always displays the latest uploaded image and there will never be a scenario when the file saved on server is different from the image that is being displayed.

Points of Interest

I still am curious about why this happened in the first place. What I could make out of it is that it is some sort of optimization mechanism that is preventing unnecessary calls when then Image control's attached file path and newly supplied file path is same. Perhaps some gurus can confirm.  

History

05  March 2012: A simple solution based on dummy query string posted.

License

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

Share

About the Author

Rahul Rajat Singh
Architect
India India
I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.

My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.
  • Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4
 
If you like my articles, please visit my website for more: www.rahulrajatsingh.com[^]
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralUseful solution Pinmemberiratus711-May-14 13:24 
GeneralMy vote of 5 Pinmemberprashant patil 49871-Jan-14 18:34 
Answerimage Pinmembershaikhshafique8-Mar-12 20:44 
we can also use this technique : increment path or filename by one or use update panel in visual studio 10 & 8
shaikh

QuestionI have noticed this 'optimisation' bug(?) as well. PinmemberMichael K Gray6-Mar-12 20:19 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 7 Mar 2012
Article Copyright 2012 by Rahul Rajat Singh
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid