Listing the Images in a Directory Demo

This demo illustrates how to use a DataList and the Directory.GetFiles() method to list the files (or images, in this case) of a directory. The downside with this demo is that the images are of varying sizes, which causes the DataList to render in an unreadable fashion. In the next live demo we'll examine how to resize the images dynamically!

Source Code
<%@ Import Namespace="System.IO" %>
<script language="vb" runat="server">

  Sub Page_Load(sender as Object, e as EventArgs)
    Const IMAGE_DIRECTORY as String = "/images/"
    Dim pics as ArrayList = new ArrayList()
    Dim s as String, html as String
    For Each s in Directory.GetFiles(Server.MapPath(IMAGE_DIRECTORY), "*.gif")    
      html = "<a href=""" & IMAGE_DIRECTORY & Path.GetFileName(s) & """>" & _
             "<img src=""" & IMAGE_DIRECTORY & Path.GetFileName(s) & """>" & _

    dlPictures.DataSource = pics
  End Sub


  <asp:DataList runat="server" id="dlPictures"  RepeatColumns="3">    
      <%# Container.DataItem %>

[Return to the Article]