Home > ASP.Net > Display All Images from Folder in DataList

Display All Images from Folder in DataList


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

Advertisements
Categories: ASP.Net
  1. Li
    February 16, 2012 at 9:10 pm

    thanks very much.

    How do I limit to increase image size?

  2. udham singh
    June 30, 2012 at 10:06 am

    Dim fileextension As String
    Dim savepath As String = “”
    Dim tempPath As String = “”
    fileextension = System.IO.Path.GetExtension(imgupload.FileName)
    Dim filename As String = CreateUsername() + fileextension
    Dim strpath As String = Server.MapPath(“~/Member_Images/”) & filename
    Dim dir As New DirectoryInfo(MapPath(“~/Member_Images/”) & filename)
    Dim file As FileInfo() = dir.GetFiles()
    Dim list As New ArrayList()
    For Each file2 As FileInfo In file
    If file2.Extension = “.jpg” OrElse file2.Extension = “.jpeg” OrElse file2.Extension = “.gif” Then
    list.Add(file2)
    End If
    Next
    DataList1.DataSource = list
    DataList1.DataBind()

    getting IO Exceptin in Dim file As FileInfo() = dir.GetFiles() line

  3. amr
    November 22, 2012 at 8:52 am

    I want hyperlink to get the name of the image… because i have a folder which have a name similar than image name
    for example:
    <asp:HyperLink ID="HyperLink1" Text='’ NavigateUrl=” runat=”server”/>
    Thank you

  4. amr
    November 22, 2012 at 8:54 am

    amr :
    I want hyperlink to get the name of the image… because i have a folder which have a name similar than image name
    for example:
    <asp:HyperLink ID=”HyperLink1″ Text=” NavigateUrl=” runat=”server”/>”/>
    Thank you

  1. No trackbacks yet.

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: