setting ImageUrl for images through code not working

Discussion in 'ASP .Net' started by mich_stone@yahoo.com, Dec 9, 2004.

  1. Guest

    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
    , Dec 9, 2004
    #1
    1. Advertising

  2. 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.)

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://Steve.Orr.net


    <> wrote in message
    news:...
    > 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
    >
    Steve C. Orr [MVP, MCSD], Dec 9, 2004
    #2
    1. Advertising

  3. aa7im Guest

    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 ());
    aa7im, Dec 9, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kathy Burke
    Replies:
    0
    Views:
    518
    Kathy Burke
    Nov 14, 2003
  2. Nick Gilbert
    Replies:
    1
    Views:
    557
    Nick Gilbert
    Feb 28, 2006
  3. DC
    Replies:
    2
    Views:
    996
  4. Nathan Sokalski
    Replies:
    2
    Views:
    530
    Nathan Sokalski
    Sep 14, 2008
  5. Nathan Sokalski

    Resource Files, Images, And The ImageUrl Property

    Nathan Sokalski, Sep 14, 2008, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    1,236
    Allen Chen [MSFT]
    Sep 16, 2008
Loading...

Share This Page