Yasserzaid’s Weblog

December 2, 2010

Display All Images from Folder in DataList

Filed under: ASP.Net — yasserzaid @ 6:06 pm

Hi all

try this example to dispaly all images in spacific folder

Suppose we have folder called “Images” in my Website contains Images with Different extensions

Step1 :- Create a new Website and add new folder called “Images” and add some images in that folder

Step2 :- Add new Web Page and Drag from Toolbox DataList control which we will dispaly images from folder in it

<asp:DataList ID="DataList1" runat="server" RepeatColumns="5" CellPadding="5">
            <ItemTemplate>
            <asp:Image Width="100" ID="Image1" ImageUrl='<%# Bind("Name", "~/images/{0}") %>' runat="server" />
                <br />
                <asp:HyperLink ID="HyperLink1" Text='<%# Bind("Name") %>' NavigateUrl='<%# Bind("Name", "~/images/{0}") %>' runat="server"/>
            </ItemTemplate>
                <ItemStyle BorderColor="Silver" BorderStyle="Dotted" BorderWidth="1px" HorizontalAlign="Center"
                    VerticalAlign="Bottom" />
</asp:DataList>

and in Code behind add this code :-


protected void Page_Load(object sender, EventArgs e)
 {
   ListImages();
 }
private void ListImages()
 {
    DirectoryInfo dir = new DirectoryInfo(MapPath("~/images"));
    FileInfo[] file = dir.GetFiles();
    ArrayList list = new ArrayList();
    foreach (FileInfo file2 in file)
     {
       if (file2.Extension == ".jpg" || file2.Extension == ".jpeg" || file2.Extension == ".gif")
        {
          list.Add(file2);
        }
     }
   DataList1.DataSource = list;
   DataList1.DataBind();
 }

Don’t forget to use namespace “Using System.IO ;”

Hope this helps

Good Luck

1 Comment »

  1. thanks very much.

    How do I limit to increase image size?

    Comment by Li — February 16, 2012 @ 9:10 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.