如何在文件夹中显示asp.net中的图像

时间:2014-08-22 19:08:21

标签: c# asp.net

我需要在asp.net datalist控件中显示文件夹中的图像。我编写了以下代码,但获得了文件名的完整路径。 这里我的代码是

// HTML元素

<asp:DataList CssClass= ID="DataList1" runat="server" RepeatColumns="5" 
        RepeatDirection="Horizontal" Width="100%">
    <ItemTemplate>
        <div class="image-container">
            <div class="image">
             <asp:Image ID="Image" runat="server" Height="150px" Width="150px" ImageUrl='<%# Container.DataItem %>' />
            </div>
            <div class="file-name">
                <asp:Label ID="Label1" runat="server" Text='<%# Container.DataItem %>'></asp:Label>
            </div>
        </div>
        </ItemTemplate>
    </asp:DataList>

//代码背后

 public void displayimage ()

    {

        string[] filesindirectory = Directory.GetFiles(Server.MapPath("~/images"));
        List<String> images = new List<string>(filesindirectory.Count());



        foreach (string items in filesindirectory)
        {
            images.Add(String.Format("~/images/{0}", Path.GetFileName(items)));
        }



        PagedDataSource obj = new PagedDataSource();
        obj.DataSource = images;
        obj.AllowPaging = true;
        obj.PageSize = 5;
        obj.CurrentPageIndex = currentpage;
        Label4.Text = "Page : " + (currentpage + 1).ToString() + " of " + obj.PageCount.ToString();
        Btn_Previous.Enabled = !obj.IsFirstPage;
        Btn_next.Enabled = !obj.IsLastPage;
        DataList1.DataSource = obj;
        DataList1.DataBind();
    }

请帮忙

0 个答案:

没有答案