Solved

Dynamically create img tag from file/image list in directory

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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