Johnny Bones
Johnny Bones

Reputation: 8402

Image inside repeater is not showing up

I've got a repeater to display photos. It's filled like so:

AcctRepeaterPhoto.DataSource = "";
dDateID = Convert.ToInt32(lDateID.InnerText);

saqConnection.conn = MySQLConn;
sqlDataAdapter adp = new SqlDataAdapter("Select * from MyTable", conn)
DataSet ds = New Dataset();
adp.Fill(ds, "MyPhotoPage");

AcctRepeaterPhoto.DataSource = ds;
AcctRepeaterPhoto.DataBind();

ds.Dispose;
conn.Close;

I'm 100% sure this works, because I use nearly identical code for 3 other parts of my form and they all work perfectly.

The ASP side, I'm not so sure about...

Most of it makes sense, but the line that's supposed to display the image is displaying nothing:

<asp:Image runat="server" ImageUrl='<%# string.Format("D:/MyPics/Photos/{0}", 
  Eval("PhotoLink")) %>' />

When I look at the source code of the generated page, it's resolving to

<img src="D:/MyPics/Photos/MyImageName.jpg">

But it's just showing that empty square that invalid images show as.

If I switch it to:

<asp:Image runat="server" ImageUrl='<%# string.Format("D:\\MyPics\\Photos\\{0}", 
  Eval("PhotoLink")) %>' />

it still only shows that empty square, but this time the source of the page shows

<img src="D:\MyPics\Photos\MyImageName.jpg">

I've checked the path, and there is a file in "D:\MyPics\Photos\" called "MyImageName.jpg".

Anyone know what's going on? Why won't this image show?

Upvotes: 0

Views: 269

Answers (2)

Amr Elgarhy
Amr Elgarhy

Reputation: 68922

You need to use a URL to your image not the file path,
ImageUrl property takes a URL

Use the ImageUrl property to specify the URL of an image to display in the Image control. You can use a relative or an absolute URL. A relative URL relates the location of the image to the location of the Web page without specifying a complete path on the server. The path is relative to the location of the Web page. This makes it easier to move the entire site to another directory on the server without updating the code. An absolute URL provides the complete path, so moving the site to another directory requires that you update the code.

You can also just pass your physical path to Server.MapPath which will return the correct url of the image if you have permission to this image folder

Something like that:

<asp:Image runat="server" ImageUrl='<%# Server.MapPath(string.Format("D:/MyPics/Photos/{0}", 
  Eval("PhotoLink"))) %>' />

Upvotes: 1

Kyrul
Kyrul

Reputation: 174

I'm not exactly sure about this but from my experience, it usually works with the ~/

so try: ~/MyPics/Photos/MyImageName.jpg

Perhaps because it's always referencing starting from the home

Upvotes: 0

Related Questions