hashim hashim - 21 days ago 5
ASP.NET (C#) Question

Images not displaying with asp.net repeater

I am trying to display pictures from folder Images with a repeater,but the probelm pictures can not display i don't know why .



<div data-u="slides" style="cursor: default; position: relative; top: 0px; left: 0px; width: 800px; height: 356px; overflow: hidden;">

<asp:Repeater runat="server" ID="RepaterImages">
<ItemTemplate>
<div runat="server" data-p="144.50">

<img id="Image" runat="server" data-u="image" style="Width:120px;" src='<%#Container.DataItem %>'/>

</div>
</ItemTemplate>
</asp:Repeater>
</div>





protected void Page_Load(object sender, EventArgs e)
{
int id = Convert.ToInt32(Request.QueryString["id"]);

var path = Server.MapPath("ProjectsImages/ ");

var images = Directory.GetFiles(path,id+"*");

ArrayList list = new ArrayList();

foreach (var img in images)
{

list.Add(img);

}
RepaterImages.DataSource = images;
RepaterImages.DataBind();
}

Answer

The problem with your code is, you are using physical path of the image. You will have to provide relative path to repeater. Change your code as below, it should work I think:

protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(Request.QueryString["id"]);
                string relativePath = "/ProjectsImages/";
                var path = Server.MapPath(relativePath);
                var images = Directory.GetFiles(path, id + "*").Select(x =>
                {
                    var arrPath = x.Split('\\');
                    string imgName = arrPath[arrPath.Length - 1];
                    return relativePath + imgName;
                });
                RepaterImages.DataSource = images;
                RepaterImages.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }