ASP.NET: Binding DataList with Images in a Directory

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_PreRender()
{
    string upFolder = MapPath("~/UploadImages/");
    DirectoryInfo dir = new DirectoryInfo(upFolder);
    dlstImages.DataSource = dir.GetFiles();
    dlstImages.DataBind();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Pic Album</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DataList id="dlstImages" RepeatColumns="3" runat="server">
        <ItemTemplate>
            <asp:Image ID="Image1" ImageUrl='<%# Eval("Name", "~/UploadImages/{0}") %>' style="width:200px" Runat="server" />
            <br />
            <%# Eval("Name") %>
        </ItemTemplate>
    </asp:DataList>
    </div>
</form>
</body>
</html>

3 comments:

  1. This post explains binding datalist with images in a directory. The method is very simple to implement. I got the logic of this program with the help of your post. I never tried this method. I follow different method for doing this but now I use this one.

    ReplyDelete
  2. very very very good...thank

    ReplyDelete