setting ImageUrl for images through code not working

  • Thread starter Thread starter mich_stone
  • Start date Start date
M

mich_stone

Dear world,

I have an IMAGE control placed on my .aspx page with its ImageUrl
property not set.

In my code, i am setting it equal to something using the following
statement

img.ImageUrl = Server.MapPath ("usedIcons/AssignAccess/" +
reader["sImage"].ToString ());

where reader as the name implies is a DataReader object reading a value
from the database.

The funny part is, when I run the code in LOCALHOST mode, it works
perfectly. But it was an embarassing experience for me, when I finally
deployed on server and asked my client to try from their remote client
machine. THE IMAGE DID NOT LOAD. All such images had the usual "X" on
them.

Why does it work on LOCALHOST, but not when viewing from a remote
client?

My doubts in case it helps you to help me:
1. Is Server.MapPath the right way to do it?
2. Is there any place in the document where this code should be kept?
3. I will be shocked if the answer to this one is "yes". "IS THE
BROWSWER LOOKING FOR THE IMAGE ON THE CLIENT MACHINE RATHER THAN ON
SERVER?"

Thanks a lot...

Michelle Stone
 
YES, the client machine is looking for the file locally, not on your server.
Server.MapPath is for working with local paths.
In this case you want a URL that points to the image, not a local path.

To verify this you can view the HTML that is sent to the browser.
(Right click on IE web page and choose View Source.)
 
When you view the images from the remote host right click on the
missing image (where the "X" is) and select properties. This will tell
you where the path is getting resolved to.... From this information
you will probably see the error....

More importantly you should not be using MapPath at all to set the
ImageUrl property. MapPath is used to find a Physical Path on the hard
drive for a file... That means the path is resolving to something like
c:\inetpub\wwwroot\myweb\images.....etc.... not good!

Just set the ImageUrl like:
img.ImageUrl = "usedIcons/AssignAccess" + reader["sImage"].ToString ());
 
Back
Top