简体   繁体   中英

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();
 }

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;
            }
        }

You have missed out the Postback property. Please use the code below.

protected void Page_Load(object sender, EventArgs e)
{
   if(!IsPostBack)
   {
        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 = list;
       RepaterImages.DataBind();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM