
Figure 1 - Slide Show Control for ASP.NET
Introduction
So where do you think this garden picture is taken? California? Maine? Florida, maybe? Believe it or not its smack in the middle of Manhattan in a section called Alphabet City in a garden community called La Plaza Cultural. The garden not only has flowers and herbs, but features an apple tree, a pear tree, and a peach tree, just to name a few. Whoever said that Manhattan was just a cement jungle, obviously never wandered into La Plaza Cultural.
The picture in figure 1 is shown in my web browser to illustrate a quick web control I threw together to allow me to view a set of pictures in a directory through ASP.NET. The secret of this control is that upon pressing the Next or Previous Button, the ImageUrl in the page is dynamically changed and the page is redirected to itself to refresh the contents. The control works as follows:
- All image filenames are read in from an image directory directly under the SlideShowControl Directory and each stored in a Session state variable
- The index of the last file is also stored in a Session variable
- Upon hitting Next, the index of the last file is incremented and the ImageUrl associated with the next index is assigned to the image control
- The page is redirected back to itself to display the next image.
This article also illustrates how to embed a user control into another user control. You would think this would be easy, but it turns out to be trickier than if you were to do it in a windows form. Upon embedding one web user control into another, I started getting circular references and had to figure out a way to prevent one user controls reference from interfering with the other user controls reference. This involved removing additional references that were inserted by the visual studio designer into both ascx (user control) files and also inheriting one of the controls from an abstract base class instead of directly from System.Web.UI.UserControl. I also placed each user control in a separate web folder so the assemblies wouldn't step on each other. I'm still not sure if one of all of these techniques solved the problem in the end, but it would be nice if the problem actually didn't happen in the first place :-).
The two user controls created in the article are SlideShowControl and SlideShowControlContainer. The SlideShowControl deals with most of the functionality of the control pertaining to the image. The SlideShowControlContainer holds the SlideShowControl and the two buttons: Previous and Next. When the Previous or Next button is pressed, the previous and next functionality calls are propogated to the SlideShowControl.
Code
The heart of the code for the SlideShow resides in the SlideShowControl.ascx.cs file. Upon loading this control, the file paths are read into the session states and the initial image is drawn.
Listing 1 - Initial Page Load of the SlideShowControl
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
// the the initial image URL
imgCurrent.ImageUrl = GetImageUrl();
if (Session["CurrentImageIndex"] == null)
{
// read in all the image paths and store in session states
IndexImages();
// Draw the image by assigning the image control to a new ImageUrl
// and redirecting the page back to itself
DrawImage();
}
}
}
The IndexImages in the SlideShowControl reads in all the images into the Session State. The Session state allows you to remember objects in your application as long as the Browser Session is alive. By persisting image file names, image indexes, and the total number of images, we can use this data throughout our session to determine which image URL needs to be shown in the Slide Show control.
The image file names are initially read in using the GetFile method of the Directory class. In this case, all files with the jpg extension are read into the Session state hash. Each filename hash value has the key (imagedirectory + unique integer value). We can use this combined key whenever we want to get the next or previous image file to assign to our Image URL in the .NET Web Image Control.
Listing 2 - Reading in all the Image Files and Assigning them to the Session
private void IndexImages()
{
// determine the actual path of the asp.net application on the server
string fullSitePath = this.Request.PhysicalApplicationPath;
// extend the path to include our slide show control image directory
string serverImageDirectory = fullSitePath + ConfigurationSettings.AppSettings["ImageDirectory"];
// save the path information in the Session State
Session["FullImagePath"] = serverImageDirectory;
// get all the files in the images directory with the desired extension
string[] files = Directory.GetFiles(serverImageDirectory, String.Format("*.{0}", _imageExtension));
// store all the file names in the session state, keyed by
// the directory name and a unique integer.
int count = 0;
Session["CurrentImageIndex"] = 0;
Session["ImageDirectory"] = _imageDirectory;
foreach (string file in files)
{
Session[_imageDirectory + count.ToString()] = file.Substring(file.LastIndexOfAny(new char[]{'/', '\\'}) + 1);
count++;
}
// store the number of images in the session state
Session["ImageCount"] = count;
}
Muruli madhavPosted Jun 3, 2012, 2:12 AM
This ia really really a fantatstic project
Muruli madhavPosted Jun 3, 2012, 2:12 AM
This ia really really a fantatstic project
Muruli madhavPosted Jun 3, 2012, 2:12 AM
This ia fantatstic project
Muruli madhavPosted Jun 3, 2012, 2:12 AM
This ia fantatstic project
Muruli madhavPosted Jun 3, 2012, 2:12 AM
This ia fantatstic project
Kishore KumarPosted Jan 14, 2009, 8:01 AM
Hi Gold, Thankyou for providing useful code. But I would like to manage the user controls with button control i.e. the user control to be loaded whenever the button click event raised. Could anyone helpout since I was at learning stage. Regards, G.V.Kishore Kumar.
Barry GoodallPosted Jan 12, 2008, 8:24 AM
Had a look at this as I wanted to add some navigation to my website for the photos. This gave me some ideas on how to go about it. I'm still new to all this and it this showed me some interesting and new techniques. Well to me anyway. Many thanks, Bazza
anitha psaPosted Oct 17, 2006, 3:16 AM
I couldn't find the string variables -imageExtension,_imageDirectory,GetCurrentFileName(); &GetDimension.X & Y. There was an error occur. Can u plz tell about these variables. Thank You.