Solved

Dynamically create img tag from file/image list in directory

Posted on 2011-09-18
4
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 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