Solved

Dynamically create img tag from file/image list in directory

Posted on 2011-09-18
4
372 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
Comment Utility
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
Comment Utility
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
Comment Utility
PERFECT!!!!!!!!!!!!!!!!!!!!
0
 
LVL 7

Expert Comment

by:NauticalNonsense
Comment Utility
Thanks! And thanks for the grade :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now