ASP.NET
  Home arrow ASP.NET arrow Page 4 - Slapping a Photo Gallery Together 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 a Photo Gallery Together in ASP.NET Part I
By: Harish Kamath
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 31
    2005-04-04

    Table of Contents:
  • Slapping a Photo Gallery Together in ASP.NET Part I
  • Say Cheese
  • Digging Deeper
  • Different Strokes
  • Etcetera
  • 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 a Photo Gallery Together in ASP.NET Part I - Different Strokes


    (Page 4 of 6 )

    Remember the idiom "there is more than one way to skin a cat"? There is no better example for this than the file management assemblies on the .NET framework.

    Why, you may wonder?

    Take a look at the Directory() (that I introduced in the earlier section) and the DirectoryInfo() (that I'll introduce in the current section) objects. The two .NET objects are similar as far as functionality is concerned. However, the methods associated with the new DirectoryInfo() object are non-static. In plain English, I must create an instance of the DirectoryInfo() class before I can invoke any of its methods, unlike for the Directory() class.

    Time to review the next code listing that introduces the DirectoryInfo() object:

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


    // custom function to return the list of photo albums
    // a.k.a folders for a particular file system location
    void GetPhotoAlbums(string strFileSystemPath) {

    // define a null object to store list of "Photo Galleries"
    DirectoryInfo[] objPhotoAlbums = null;

     try {

    // create an instance of the DirectoryInfo() object
    // on the basis of the file-system path provided
      DirectoryInfo objCurrentPhotoAlbum = new DirectoryInfo(strFileSystemPath);

      output.Text += "<UL>";

     // use the GetDirectories() method
     // to obtain a list of sub-folders
      objPhotoAlbums = objCurrentPhotoAlbum.GetDirectories();

    // iterate over collection of DirectoryInfo objects
     // get details of each "Photo Album"
      foreach(DirectoryInfo objPhotoAlbum in objPhotoAlbums) {

       output.Text += "<LI> Photo Album: <A HREF=\"example.aspx?strPhotoAlbumPath=" + objPhotoAlbum.FullName + "\">" + objPhotoAlbum.Name + "</A>";

      }

     // define array to store list of "Photos"
     string[] aryPhotos;

      // use the GetFiles() static method
     // to obtain a list of image i.e. JPEG files
      aryPhotos = Directory.GetFiles(strFileSystemPath, "*.jpg");

    // iterate over array of "Photos" to
     // get details of each Photo
      foreach(string strPhoto in aryPhotos) {

       output.Text += "<LI> Photograph Name: <U>" + strPhoto.Substring(strFileSystemPath.Length + 1)  + "</U>";

    }

     } catch (Exception e) {

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

    void Page_Load(Object sender, EventArgs e) {

    // path to the root folder of the "Photo Gallery"
    string strPhotoGalleryRoot = "E:\\inetpub\\wwwroot\\Gallery";

    // path to the current folder a.k.a Photo Album
    string strCurrentPhotoAlbumPath = "";

    if(Request.QueryString["strPhotoAlbumPath"] == null) {

    // no value in query string
    // start at root level of "Photo Gallery"
      strCurrentPhotoAlbumPath = strImageGalleryRoot;

     } else {

      // enable the "Up One Folder Link"
      // only if we are NOT at the root folder level
      if(Request.QueryString["strPhotoAlbumPath"] != strImageGalleryRoot) {

        back.Visible = true;
        back.Text = "<A HREF=\"example.aspx?strPhotoAlbumPath=" + Request.QueryString["strPhotoAlbumPath"].Substring(0, Request.QueryString["strPhotoAlbumPath"].LastIndexOf("\\")) + "\">Up One Level</A>";
      }

      // escape the "\" character from the query string value
      strCurrentPhotoAlbumPath = Request.QueryString["strPhotoAlbumPath"].Replace("
    \\","\\\\");
     }

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

     // get the Photo Albums and Photos under current Photo Album
     GetPhotoAlbums(strCurrentPhotoAlbumPath);
    }
    </SCRIPT>
    <HTML>
    <HEAD>
        <TITLE>My Gallery</TITLE>
        <BASEFONT face="Arial" />
    </HEAD>
    <BODY>
        <asp:Label id="output" runat="server" style="line-height:25px" />
        <BR /><BR/>
        <asp:Label id="back" runat="server" visible="true" />
    </BODY>
    </HTML>

    And here is the output:

    One of the most noticeable changes in the output is the association of a hyperlink with each Photo Album in the listing. Click on a Photo Album at the root level and the script will immediately display a list of more Photo Albums (a.k.a. sub-folders) and Photos (a.k.a. the image files) present under the current Photo Album (a.k.a. folder).

    A resourceful "Up One Level" hyperlink allows the viewer to navigate back to the parent folder with great ease.

    The programming logic behind this example is straightforward - I've set up the script to recursively pass the file-system path to the selected Photo Album (a.k.a. folder) in the query string. The same script, then, retrieves this path using the "QueryString" property of the Request() object. If there is no such value - for example, when you load the script for the first time - the script defaults to the root folder of the "Photo Gallery."

    Note that you may have to escape the "backslash" character from the file-system path in order to avoid any unforeseen errors.

    This is followed by some deft string manipulation in order to retrieve the name of the current Photo Album (a.k.a. folder).

    Once again, I've resorted to my custom GetPhotoAlbums() function. However, this time around the function has been totally re-written in order to leverage on the properties and methods of the DirectoryInfo() object. 

    To begin with, I've defined an array titled "objPhotoAlbums" to a collection of DirectoryInfo() objects. The purpose remains the same as in the earlier example, only the data type to be stored in the array is different.

    Within the resourceful try-catch-finally block, I've created my first instance of the DirectoryInfo() object. Note that you have to pass the file-system path as an input parameter to the constructor of the object. If the path does not exist, the script will immediately throw an exception and exit, as shown below.

    But if the directory exists, the script invokes the GetDirectories() method of the DirectoryInfo(). This is where the two objects, i.e. the Directory() and DirectoryInfo() objects, differ in their behavior. Much to the satisfaction of OOPs purists, this GetDirectories() method returns a collection of DirectoryInfo() objects - one for each sub-folder.
    I can easily obtain the names of each sub-folder (representing a "Photo Album") using the "Name" property of the DirectoryInfo() object. However, I need the complete path to the sub-folder in order to generate the required hyperlink. No sweat; the DirectoryInfo() object is equipped with a "FullName" property that gives me the entire path to the sub-folder, including the starting drive letter.

    Note that I have re-used the static GetFiles() method of the Directory() object in order to retrieve the image files present in the current folder. There is no sinister motive behind this move; only an attempt to keep things simple.

    In the next section, I will take a little detour from my "Photo Gallery" application and show you how to create folders using this new DirectoryInfo() object.

    More ASP.NET Articles
    More By Harish Kamath


     

    ASP.NET ARTICLES

    - 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...
    - Building an In-Text Advertising System Under...
    - Developing a Mini ASP.NET AJAX Server Centri...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT