Results 1 to 8 of 8

Thread: [2005] DataList image problem [RESOLVED]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271

    Unhappy [2005] DataList image problem [RESOLVED]

    Hi,

    I currently have a datalist, which displays an image.

    Currently the image is gained using:

    Code:
    <%# DataBinder.Eval(Container.DataItem, "stock_id", "images/stock/{0}.jpg") %>
    in the HTML, as you can see the images are stored in a directory not a DB, and they use the stock items ID. At the moment if the image doesnt exist it just shows the alt text.

    Is there something I can use on ItemDataBound to set the image to images/none.jpg if the image doesnt exist?

    I know I can use to FindControl to sweep over the list, but this assigns the image to ALL the images, which isnt what I want.

    Many Thanks,
    Last edited by §tudz; Apr 16th, 2007 at 02:22 PM.
    §tudz

    Studzworld.com - Portfolio

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] DataList image problem

    In the ItemDataBound, for each row, you will know the ID. Ergo, you will know the location of the image you want to display. Assuming the images are on the same server, you can do a simple

    System.IO.File.Exists()

    to find out if the image is in the directory or not. If it's not, show none.jpg.

    If the images are not on the same server, there's another way, but I believe that this is pretty much what you're looking for.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271

    Re: [2005] DataList image problem

    thanks, can that be used with http dir or do I need the complete route name (c:/../mywesite/images/..)

    Thanks for the code I'll give it a try tonight.
    I have found some javascript that looks at every image and if the image isnt there it shows the none.jpg but this is very hacky and clunky as it will show the none.jpg for every image on the page that doesnt show, so for instance if something happened in communication with the server and the image didnt show (like sometimes happens on the web) it would show the none.jpg instead of the logo or such.
    §tudz

    Studzworld.com - Portfolio

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] DataList image problem

    It can be done with http, it can be done with the path.

    The path would be easier. System.IO.File.Exists(). Look it up on MSDN. You don't even need to hardcode the path, you can simply say

    Code:
    System.IO.File.Exists(Server.MapPath("~/images/something.jpg"))
    But wait, you know that already don't you?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271

    Re: [2005] DataList image problem

    cool thanks I shall have a read much better than the javascript

    cheers
    §tudz

    Studzworld.com - Portfolio

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271

    Re: [2005] DataList image problem

    after some tweaking that worked great,

    here is the code I used:
    vb Code:
    1. If Not System.IO.File.Exists(Server.MapPath(DirectCast(e.Item.FindControl("img_Stock"), HtmlImage).Src.ToString())) Then
    2.             DirectCast(e.Item.FindControl("img_Stock"), HtmlImage).Src = "images/none.jpg"
    3.         End If
    §tudz

    Studzworld.com - Portfolio

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] DataList image problem

    Glad to know, now add Resolved!

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271

    Re: [2005] DataList image problem [RESOLVED]

    lol, I'm always getting shouted at for that I always forget to do it XD
    §tudz

    Studzworld.com - Portfolio

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width