简体   繁体   中英

Display images from local folder to datalist using Asp.net

I am displaying images from my application to datalist.Source code is:

<asp:DataList ID="dtlist" runat="server" RepeatColumns="4" CellPadding="5">
<ItemTemplate><asp:Image Width="100" ID="Image1" ImageUrl='<%# Bind("Name", "~/Images/{0}") %>' runat="server" />
<br /></ItemTemplate>
<ItemStyle BorderColor="Brown" BorderStyle="dotted" BorderWidth="3px" HorizontalAlign="Center"VerticalAlign="Bottom" />`
</asp:DataList>

code behind is

  protected void BindDataList()
{
    DirectoryInfo dir = new DirectoryInfo(MapPath("Images"));
    FileInfo[] files = dir.GetFiles();
    ArrayList listItems = new ArrayList();
    foreach (FileInfo info in files)
    {
        listItems.Add(info);
    }
    dtlist.DataSource = listItems;
    dtlist.DataBind();

}
protected void btnsave_Click(object sender, EventArgs e)
{
    string filename = Path.GetFileName(fileupload1.PostedFile.FileName);
    fileupload1.SaveAs(Server.MapPath("Images/" + filename));
    BindDataList();

}

As this I want to display all the images from local drive to data list. ex from C:\\Sales n purchases\\ImagesFolder to datalist.

please any one help me. Thanks in advance.

Please try with the below code snippet.

<ItemTemplate>
        <asp:Image Width="100" ID="Image1" ImageUrl='<%# String.Format("{1}\\{0}", Eval("Name"), Eval("Directory")) %>'
            runat="server" />
        <br />
        <asp:Label id="lblImageSrc" runat="server" Text='<%# String.Format("{1}\\{0}", Eval("Name"), Eval("Directory")) %>'></asp:Label>
    </ItemTemplate>

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