With the use of the following code you can retrieve all images from any folder and display them in a form using a DataList control of ASP.Net.
To retrieve images from a folder this article provides a few steps which will be easy to follow.
Step 1:
Design a Form; drag a DataList Control from the toolbox onto the form. Set the Repeated Column property and design the DataList control as you need. I am using an image button and a link button in my form.
<asp:DataList ID="DataList1" runat="server" RepeatColumns="5" BackColor="White"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black"
Width="100%">
<FooterStyle BackColor="#CCCCCC" />
<SelectedItemStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderTemplate>
<span class="style2">Image Gallary</span>
</HeaderTemplate>
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<ItemTemplate>
<asp:ImageButton Width="105px" ID="Image1" runat="server" BorderStyle="Solid" ImageUrl='<%# Bind("Name", "~/[foldername]/{0}") %>'
Height="94px" />
<br />
<asp:LinkButton ID="HyperLink1" Text='<%# Bind("Name") %>' CommandArgument='<%# Bind("Name") %>' runat="server" />
</ItemTemplate>
<FooterStyle BackColor="White" ForeColor="#333333" />
<ItemStyle BorderColor="Silver" BorderStyle="Dotted" BorderWidth="1px" HorizontalAlign="Center"
VerticalAlign="Bottom" BackColor="White" ForeColor="#333333" />
</asp:DataList>
Step 2:
Write the function below and call it in the Form Load event.
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" || file2.Extension == ".png")
{
list.Add(file2);
}
}
DataList1.DataSource = list;
DataList1.DataBind();
}
Step 3: Run your website........

Manoj ChouksePosted Jul 7, 2015, 8:15 AM
I did use your code but i am getting error on this code Error 1 The type or namespace name 'DirectoryInfo' could not be found (are you missing a using directive or an assembly reference?) Error 2 The type or namespace name 'DirectoryInfo' could not be found (are you missing a using directive or an assembly reference?) Error 3 The type or namespace name 'FileInfo' could not be found (are you missing a using directive or an assembly reference?) Error 4 The type or namespace name 'ArrayList' could not be found (are you missing a using directive or an assembly reference?) Error 5 The type or namespace name 'ArrayList' could not be found (are you missing a using directive or an assembly reference?) Error 6 The type or namespace name 'FileInfo' could not be found (are you missing a using directive or an assembly reference?) Error 7 Cannot implicitly convert type 'ArrayList' to 'object'
Kamal SamantPosted Jun 25, 2013, 2:39 AM
This article allows only working directory folder ,how do we achieve for random Directory(F/Img) eatc.????
Amit TiwariPosted Dec 7, 2012, 4:50 AM
Hello Sir, this article is very useful for me. but i have a problem i want to display name of images without file extension. how it will done? please help me.
Amit TiwariPosted Dec 7, 2012, 4:49 AM
Hello Sir, this article is very useful for me. but i have a problem i want to display name of images without file extension. how it will done? please help me.
Dhanya JoyPosted Jul 6, 2012, 2:23 AM
Thanks for the post! Talentica helps technology companies transform their ideas into successful products. To know more, visit http://www.talentica.com or write to me at [email protected].
losen dreamPosted Mar 9, 2012, 3:22 PM
if i need to send foldername in (ImageUrl='<%# Bind("Name", "~/[foldername]/{0}") %>') from c# code because I'll take it from user.....thnx
losen dreamPosted Mar 9, 2012, 3:18 PM
if i need to send foldername in (ImageUrl='<%# Bind("Name", "~/[foldername]/{0}") %>') from c# code because I'll take it from user.....thnx
losen dreamPosted Mar 9, 2012, 3:17 PM
if i need to send foldername in (ImageUrl='<%# Bind("Name", "~/[foldername]/{0}") %>') from c# code because I'll take it from user.....thnx
SHASHIKANT BAGALEPosted Mar 17, 2011, 4:52 AM
nice article
NickPosted Mar 13, 2011, 2:00 PM
it runs without error but cannot view the images, there is a X where the image should be, any suggestion to fix this?
SHASHIKANT BAGALEPosted Mar 10, 2011, 1:32 AM
very nice article please send such type of article sir i am very happy for this article PLEASE INFORM ME WHEN YOUR NEXT ARTICLE POST .I AM WAITING. THANKS
Blocked AccountPosted Mar 8, 2011, 10:50 PM
Ankit keep it up!
Kiran KaralePosted Mar 8, 2011, 4:52 AM
It's a good artical