ASP.NET Datalist图像不显示

时间:2011-07-18 12:23:19

标签: c# asp.net datalist

我正在使用Datalist在文件夹中显示图像。代码如下:

 <div>
    <asp:DataList ID="DataList1" runat="server" RepeatColumns="3">
        <HeaderTemplate>
        </HeaderTemplate>
        <ItemTemplate>
           <img src = "<%# Container.DataItem %>" />
        </ItemTemplate>
        <FooterTemplate>
        </FooterTemplate>
    </asp:DataList>
</div>
代码背后的代码:

   protected void Page_Load(object sender, EventArgs e)
    {
        string[] list = Directory.GetFiles(Server.MapPath("sony"));

        List<string> aList = new List<string>();
        foreach (var rec in list)
        {
            aList.Add( rec);
        }
        DataList1.DataSource = aList;
        DataList1.DataBind();
    }

它不显示图像。请帮忙

1 个答案:

答案 0 :(得分:1)

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string[] list = Directory.GetFiles(Server.MapPath("sony"));

        var aList = from fileName in Directory.GetFiles(Server.MapPath("sony"))
                    select string.Format("/sony/{0}", Path.GetFileName(fileName));

        DataList1.DataSource = aList;
        DataList1.DataBind();
    }
}