2008/10/07

Refresh the ASP.Net Image control (6/17/2009 updated)

A very simple request: Refresh the Image control on your aspx page when you want to.
Scenario:
1.You want to refresh a specific image when a user clicks a button.
2.You want to refresh a specific image when that image has been changed.
3.You want to refresh a specific image periodically.

This won't work:
//strImagePath is a variable which stores the image path and filename
Image1.ImageUrl = strImagePath;

This will:
Image1.Attributes.Add("src", "strImagePath?ts=" + System.DateTime.Ticks);

In order to avoid the browser sending the old/cached image to client, we can change the value of the image's "src" property by giving a timestamp. We can also change the image file name (like image20090617.jpg -> image20090618.jpg -> image20090619.jpg), but this will require more steps and increase the complexity.

7 comments:

  1. It help me to solve the problem. Thanks a lot!

    ReplyDelete
  2. Thank you so much! It solved mine too!

    ReplyDelete
  3. Thank you so much Kenny.Its solved my problem.

    10x a lot
    Parmesh

    ReplyDelete
  4. Thank you very much...

    ReplyDelete
  5. Thanks a ton Friend. This one saved me on my B'Day.

    ReplyDelete
  6. I have store Images in Database column, I am displaying all data in to grid view when I click on particular record on grid I am showing Photo of particular record on Page Image control. But when showing image all page is refreshing.

    ReplyDelete