ASP.NET
  Home arrow ASP.NET arrow Page 4 - Slapping Together a Photo Gallery in ASP.N...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Slapping Together a Photo Gallery in ASP.NET Part II
By: Harish Kamath
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 22
    2005-04-11

    Table of Contents:
  • Slapping Together a Photo Gallery in ASP.NET Part II
  • Working with Files
  • Of Albums and Photos
  • Two Sides of a Coin
  • My Gallery
  • My Gallery - Demystified
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Slapping Together a Photo Gallery in ASP.NET Part II - Two Sides of a Coin


    (Page 4 of 7 )

    In the previous article, I introduced two similar objects to manage folders on the .NET platform - the Directory() and DirectoryInfo() objects. So, it should not come as a surprise to learn  that a similar duality exists for managing files on the server.

    It's time to say hello to the FileInfo() object, a counterpart of the DirectoryInfo() object, introduced earlier. Take a peek at the next code listing that introduces this new object and a whole lot more:

    <%@ Page Language="C#" Debug="true" %>
    <%@ Import Namespace="System.IO" %>
    <SCRIPT language="C#" runat="server">

    // custom function to return the list of photographs
    // a.k.a. image files for a particular file system location
    void GetPhotos(string strFileSystemPath) {

        // define array to store list of "Photos"
     FileInfo[]  objPhotos;

     try {

        // instantiate a DirectoryInfo() object
      DirectoryInfo objCurrentAlbum = new DirectoryInfo
    (strFileSystemPath);
      
         // use the GetFiles() method
            // to obtain a list of photos i.e. image files
      objPhotos = objCurrentAlbum.GetFiles("*.jpg");

            if(objPhotos.Length != 0) {

             // there are photos in the current album
             // enable the photo display panel
             pnlPhotos.Visible = true;

             foreach(FileInfo objPhoto in objPhotos) {

              // check if the photo i.e. image file exists
              if(objPhoto.Exists) {

               // if it does, add a Image() object
               // to Photo Panel
         Image objImage = new Image();
            objImage.ImageUrl = objPhoto.FullName.Substring
    (Request.PhysicalApplicationPath.Length) ;
            objImage.Width = 200;
            objImage.Height = 145;
            pchPhotos.Controls.Add(objImage);
            pchPhotos.Controls.Add(new LiteralControl("<HR
    width=\"25%\" align=\"left\">"));

                    }
                }

            } else {
          error.Text = "Sorry, there are no photographs in
    the current Photo Album.";
            }

     } catch (Exception e) {

      error.Text = "An exception occurred: " + e.Message;
     }
    }

    void Page_Load(Object sender, EventArgs e) {

         // path to a particular Photo Album
         // i.e. folder on the file system
         string strPhotoAlbumPath = "E:\\inetpub\\wwwroot\\Gallery\\London";

         // display name of current Photo Album
         int intStartPosition = strPhotoAlbumPath.LastIndexOf("\\") + 1;
         int intLength = strPhotoAlbumPath.Length -
    intStartPosition;
         output.Text += "You are currently viewing the <U>"
    + strPhotoAlbumPath.Substring(intStartPosition,
    intLength) + "</U> Photo Album.";


         // check if the specified Photo Album i.e. folder exists
         if(Directory.Exists(strPhotoAlbumPath)) {

              // get the list of photos
              // in a particular Photo Album
    GetPhotos(strPhotoAlbumPath);

         } else {

              error.Text = "Sorry, the folder <U>" +
    strPhotoAlbumPath + "</U> could not be located.";
         }
    }
    </SCRIPT>
    <HTML>
    <HEAD>
        <TITLE>My Gallery</TITLE>
        <BASEFONT face="Arial" />
    </HEAD>
    <BODY>
        <!-- Panel for Photos -->
        <asp:Panel id="pnlPhotos" visible="false" runat="server" >
            <asp:Label id="output" runat="server"  />
            <BR /><BR />
            <asp:Placeholder id="pchPhotos"
    runat="server" />
        </asp:Panel>
        <!-- Error label -->
        <asp:Label id="error" runat="server" visible="true"
    style="color:#FF0000;"/>
    </BODY>
    </HTML>

    Load the above example in your browser to view following output:

    For the first time, I've updated the code listing to display the image files rather than just names, sizes and other useless information that I've been doing so far.

    While the above example appears to be daunting at first glance, it is my job to de-mystify the complexities. Let’s do that by breaking the code listing into smaller bits. Concentrate on the HTML portion of the ASP.NET script. Here, you'll notice that I have introduced a couple of ASP.NET server controls: first, the "Panel" control allows you to group several .NET server controls together and next, the "Placeholder" control defines a placeholder to add new server controls at run-time.

    The Page_Load() function has been excerpted from an example in the previous article. There are two important points to keep in mind. First, I've defined a string variable titled "strPhotoAlbumPath" to store the file system location of a selected "Photo Album." Second, I've obtained the name of the current "Photo Album," i.e. folder, using some deft string manipulation.

    Next, I've revamped the ubiquitous GetPhotos() function: here, I've a defined an array titled "aryPhotos" to store a collection of FileInfo() objects, unlike the earlier example, where a similar array stored string values. Next, I've instantiated a DirectoryInfo() object and invoked its GetFiles() method. This returns a collection of FileInfo() objects that I store in the aforementioned "aryPhotos" array. Note that each object in this collection represents a "Photo," i.e. an image file.

    As usual, the FileInfo() object exposes many properties that provide useful information about the file. You can view the entire list at the following URL: http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemIOFileInfoClassTopic.asp

    In the above example, I've obtained the absolute file system path by using the "FullName" property of the FileInfo() object and the "Request.PhysicalApplicationPath" property. I've calculated the path of every image file, relative to the root folder of the application. This comes in handy when I assign the URL for every Image() object that I've added to my "pchPhotos" placeholder server control.

    The "LiteralControl" server control allows me to insert custom text (including HTML code) within the "pchPhotos" placeholder. I've used it to insert horizontal lines after each photograph, as seen in the output.

    Note the use of "if-else" and "try-catch" blocks to trap errors if something goes wrong; it is a good practice to expect the unexpected.

    More ASP.NET Articles
    More By Harish Kamath


     

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek