Click here to Skip to main content
12,299,947 members (49,482 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

4.6K views
3 bookmarked
Posted

Dynamically Populating Your .aspx Page with Images Direct from Directories

, 15 Apr 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Here, we are getting all/filtered images from our directory to our .aspx page

Introduction

Here, we don't want to include the img tag and then include src to show images on our WebPage. All this is done by code-behind.

So how will it be useful. Here are the advantages.

Suppose you have made a page for a client in which images are shown of his/her company and after some period of time, your client gives you a call saying that they have some new pictures and you should just upload them too. Now what? You have to first get those images and put them in the online folder of that client website and then take that page do some edit and update that page.

By using the code below, we can add dynamically, just add the new images or replace the old one in your Images Folder/Directory, that's all and the web page will automatically add/update them.

Background

You need to have a little bit knowledge of:

  • using System.IO;
  • using System.Web;

Using the Code

Here we are getting all the Images/files from our directory from code-behind by using:

Directory.GetDirectories Method (Path, SearchPattern, SearchOption) 

path - Here path is the folder/directory where your files/Images are located.

*(searchPattern) - searchPattern can be a combination of literal and wildcard characters, but doesn't support regular expressions.

Example: If we want only png files, then we will use as - *.png or if we only want jpeg then - *.jpeg

SearchOption - One of the enumeration values that specifies whether the search operation should include all subdirectories or only the current directory.

  1. TopDirectoryOnly - Includes only the current directory in a search operation.
  2. AllDirectories - Includes the current directory and all its subdirectories in a search operation. This option includes reparse points such as mounted drives and symbolic links in the search.
string imagepath = Server.MapPath("~/Slider/Images/");
          string[] images =Directory.GetFiles(imagepath,"*",SearchOption.TopDirectoryOnly);

          foreach (string filepath in images)
          {

            //Creating Image Control
              Image te = new Image();
              //Getting File Name of Image from our Directory
      string fileurl = Path.GetFileName(filepath.ToString());

              te.ToolTip = fileurl;
              te.ImageUrl = "~/Slider/Images/" + fileurl;
              te.Height = 100;
              te.Width = 200;
              te.CssClass = "zoom";
              //Here myimages is div in which all images will be added.

              myimages.Controls.Add(te);
          }

You can access your div by adding runat=server in it.

For example:

<div id="your_div_name" runat="server"></div> 

That's all. Use the code and save time.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Narendra Singh Rathore
Software Developer Pravish Technologies
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Volynsky Alex20-Apr-14 3:16
professionalVolynsky Alex20-Apr-14 3:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 15 Apr 2014
Article Copyright 2014 by Narendra Singh Rathore
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid