Solved

Dynamically create img tag from file/image list in directory

Posted on 2011-09-18
4
375 Views
Last Modified: 2012-06-27
I have the below - sime image list inside a div - is there a way - in .NET/C# - to take all the images out of a directory and generate a dynamic image list as is per the static representation below - in something like maybe a placeholder, etc.?

 <div id="gallery">
                <img src="../gallery/images/p1.jpg" alt="" title="Portraits" />
                <img src="../gallery/images/p2.jpg" alt="" title="Portraits" />
            </div>
0
Comment
Question by:tbaseflug
  • 3
4 Comments
 
LVL 7

Accepted Solution

by:
NauticalNonsense earned 500 total points
ID: 36557522
Greets

You'll need the System.IO namespace.
Put a literal on your ASPX page
<div>
        <asp:Literal ID="litDivOnASPXPage" runat="server" />
    </div>

And that should do it.

protected void Page_Load(object sender, EventArgs e)
    {
        ShowImagesInDirectory("/images");
    }


    private void ShowImagesInDirectory(string path)
    {
        string literalDiv = "<img src=\"" + path + "\\" + "%FILE%" + "\"" + " alt=\"\" title=\"Portraits\">";
        string completeOutput = "";

        DirectoryInfo directory=new DirectoryInfo(Server.MapPath(path));
        foreach (FileInfo file in directory.GetFiles("*.JPG"))
        {
            // or you can check the extension if you wanna iterate thru all of them
            // if (file.Name.ToUpper().EndsWith("JPG"))
            // {
                completeOutput += literalDiv.Replace("%FILE%", file.Name);
            // }
        }
        litDivOnASPXPage.Text = completeOutput;
    }

Open in new window

0
 
LVL 7

Expert Comment

by:NauticalNonsense
ID: 36557528
the literalDiv guy is broken out up there, you can put whatever markup you want in it, I just did the basics.
Cheers
0
 

Author Closing Comment

by:tbaseflug
ID: 36557721
PERFECT!!!!!!!!!!!!!!!!!!!!
0
 
LVL 7

Expert Comment

by:NauticalNonsense
ID: 36557738
Thanks! And thanks for the grade :)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question