Solved

Dynamically create img tag from file/image list in directory

Posted on 2011-09-18
4
378 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
parse string in c# 5 40
Set arraylist as datatable 2 29
search by any word in column 5 29
Trying to check to see if email is in valid format in asp.net 3 21
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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