Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Discover the top 5 tips for understanding .NET Interop
Search :       Advanced Search »
Home » ASP.NET & Web Forms » Using Lightbox in an ASP.NET Application (C#)

Using Lightbox in an ASP.NET Application (C#)

The article describes Lightbox as, "... a simple, unobtrusive script used to overlay images on the current page." It delivers a nice, professional looking method for displaying images as overlays through the use of hyperlinks.

Author Rank :
Page Views : 68785
Downloads : 2721
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
LightBoxDemoCS.zip
 
 
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 



Introduction

Lightbox 2 is a free download made available by Lokesh Dhakar on this website:

http://www.huddletogether.com/projects/lightbox2

The site describes Lightbox as, "... a simple, unobtrusive script used to overlay images on the current page." It delivers a nice, professional looking method for displaying images as overlays through the use of hyperlinks. Implementation of Lightbox is quite simple involving nothing more that adding some JavaScript and a style sheet to the page and then adding some simple attributes to each link where the effect is to be used. The download includes all of the images, style sheets, and JavaScript necessary to implement Lightbox.

Lightbox allows the developer the option of displaying images singly or in sequence. Images grouped into sequences may be viewed by the user in a slideshow like presentation that the user can control with either the mouse or the left and right arrow keys. All in all, it is a nice way to present a image gallery of thumbnails. As each image is displayed, the control is resized to fit the image and the image is centered on the page. If the image is too large, it will overrun the page so it is important to resize the images so that they will fit within the space available. Once the image is displayed, the user may click off the image or hit the Escape key in order to return to the page.



Figure 1: Lightbox in Use within an ASP.NET Web Application



Figure 2: Lightbox at Rest within an ASP.NET Web Application
(Clicking a link or an image will launch the image into Lightbox)

Getting Started

In order to get started, unzip the included project and save it to your hard drive. Open the web application and examine the contents in the solution explorer.



Figure 3: Solution Explorer

The solution contains a single web application entitled, "LightBoxDemo". The solution contains three added folders: css, images, and js. These folders contain the style sheet, images, and JavaScript provided with the Lightbox download. Additional images were added to the images folder to support the demonstration project. If starting a new project, just create the three folders using these folder names and then copy the contents over into each of the folders from the Lightbox 2 download.

Code: Default.aspx.cs

No additional code was provided to the default page's code behind file. It is in its default condition and is shown here to illustrate that no coding is required to make use of Lightbox 2.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
   {

     }
}


Code: Default.aspx

What little work is required to implement Lightbox 2 is contained in the Default.aspx file itself.

The markup starts with the following unmodified, default code:

<%@ Page="" Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

The first bit of actual work is contained in the head section of the markup; here the JavaScript is references as is the style sheet:

<head runat="server">
       <%--javascript references="" for="" lightbox--=""%>
        <script type="text/javascript" src="js/prototype.js"></script>
        <script type="text/javascript"
        src="js/scriptaculous.js?load=effects,builder"></script>
       <script type="text/javascript" src="js/lightbox.js"></script>
         <%--stylesheet for="" lightbox--=""%>
          <link rel="stylesheet" href="css/lightbox.css" type="text/css"
         media="screen" />
           <%--page title--=""%>
           <title>Lightbox Demo</title>
</head>

Next up, within the form, the first example shows using a simple link to an image; the "rel" tag defines the relationship to the Lightbox style sheet and that is pretty much all that you need to do to evoke the effect.

<body bgcolor="Black"> 
      <
form id="form1" runat="server" style="background-color: #000000; color:
FFFFFF">
         <%--single image="" hyperlink--=""%>
          <div>
            Single Image<br /><br />
            <br />
            <
a href="images/image-1.jpg" rel="lightbox" title="Plant
Life">Look at This Plant</a>
            <br /><br />
          </
div>

The next example shows how to setup a sequence of images. The defined sequence may be controlled by the user with either the mouse or the left and right arrow keys (to move back and forth through the images). Note the passing of "roadtrip1" to Lightbox; all links using 'roadtrip1' will be treated as a group of images. In this case, there are three images using that name and so those three images are treated as a single series; other images on the page that do not use 'roadtrip1' are ignored. This example as well as the next both display thumbnail versions of the image.

   <%--using multiple="" images="" in="" a="" sequence="", link="" is="" thumbnail="" of="" image--=""%>
            <div>
              Raccoons<br /><br />
 
              <
a href="images/bottletree1.JPG" rel="lightbox[roadtrip1]"
              title="Raccoons and Baby Bottles">
                <img src="images/bottletree1.JPG" width="60" height="60" alt="" />
             </
a>

               <a href="images/bottletree2.JPG" rel="lightbox[roadtrip1]"
              title="Sugar Water Bottles">
                <img src="images/bottletree2.JPG" width="60" height="60" alt="" />
             </
a>

               <a href="images/bottletree3.JPG" rel="lightbox[roadtrip1]"
              title="More Sugar Water">
                <img src="images/bottletree3.JPG" width="60" height="60" alt="" />              </a
            </
div>
            <
br />
            <
br />
            <
br />
            <
br />

The last example shows how to setup a second sequence of images; note that these images are banded together as 'roadtrip2' and as such they will be kept separate from the other images on this page when viewed using Lightbox:

            <%--using multiple="" images="" in="" a="" sequence="", link="" is="" thumbnail=""
of="" image--=""%>
              <div>
                Birds<br /><br />

                <a href="images/AmericanGoldfinch_male.jpg" rel="lightbox[roadtrip2]"
                title="American Goldfinch">
                  <img src="images/AmericanGoldfinch_male.JPG" width="60" height="60"
                  alt="" />
               </a>

                 <a href="images/birdgroup1.jpg" rel="lightbox[roadtrip2]"
                title="Mixed Birds">
                  <img src="images/birdgroup1.JPG" width="60" height="60" alt="" />
                </a
                <
a href="images/doves.jpg" rel="lightbox[roadtrip2]"
                title="Mourning Doves">
                  <img src="images/doves.JPG" width="60" height="60" alt="" />
                </a>
                <
a href="images/goldfinch_fiesta.jpg" rel="lightbox[roadtrip2]"
                title="American Goldfinches">
                  <img src="images/goldfinch_fiesta.JPG" width="60" height="60" alt="" />
                </
a>

                <a href="images/hummingbird.jpg" rel="lightbox[roadtrip2]"
                title="Ruby Throated Hummingbird">
                  <img src="images/hummingbird.JPG" width="60" height="60" alt="" />
                </
a>

                <a href="images/RoseBreastedGrosbeak_male.jpg"
                rel="lightbox[roadtrip2]"
                title="Rose Breasted Grosbeak">
                  <img src="images/RoseBreastedGrosbeak_male.JPG" width="60"                   height="60" alt="" />
                </a>

             </
div>
            </form>
     </body>
  </
html>

Summary

Lightbox 2 is sort of a fun little tool that is very useful if one wants to display images from either links or thumbnail versions of an image. I thought it was a good find that simplified the process of displaying a large or small number of images in an interesting way and of course the price is perfect.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 Article Extensions
Contents added by Engin Kaya on Jan 15, 2012
Hello,
it does not work on IE. Do I make something wrong?
Thx.

 [Top] Rate this article
 
 About the author
 
Scott Lysle
Freelance software developer residing in Alabama. Bachelors, Masters Degrees from Wichita State University. I spent the first half of my career working on aircraft controls and displays and in that time I worked on the cockpits for the OH-58 AHIP, the AH-1W, the V-22, the F-22, the C-130J, the C-5 AMP, AWACS, JPATS, and a few others. Since 1997 I have been largely involved with Windows and web development, GIS application development, consumer electronics development (embedded linux/java), but still sometimes work on aircraft and military projects, the most recent of which was the presidential transport helicopter. I tend to work primarily with C/C++, Java, VB, and C#.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
asp.net by sherwin On May 4, 2009
I will try
Reply | Email | Modify 
Jquery by gagga On May 25, 2009
Is the same as the jQuery plugin, http://leandrovieira.com/projects/jquery/lightbox/ ?


Reply | Email | Modify 
Re: Jquery by Scott On May 26, 2009
It was not at time so far as I know but it is not a surprise to see it as a jquery plug-in.
Reply | Email | Modify 
Lightbox Projects List by freezesoul On May 26, 2009
Reply | Email | Modify 
Re: Lightbox Projects List by Scott On May 26, 2009
Interesting stuff; I liked the Lightbox gone wild demo.
Reply | Email | Modify 
thank you Scott Lysle by mvpajun On May 26, 2009
hi,this demo is very cool,i must say that thank you by my heart .
and i like this articles .
Reply | Email | Modify 
Re: thank you Scott Lysle by Scott On May 27, 2009
Thanks
Reply | Email | Modify 
lightbox by ashraf On May 27, 2009
i want to use this demo in my project to school. do i have the rights to do that?
and thank u for a nice article
Reply | Email | Modify 
Re: lightbox by Scott On May 27, 2009
Sure, you can use it anyway you see fit.
Reply | Email | Modify 
Re: Re: lightbox by laxman On May 31, 2010
its not working in master page
Reply | Email | Modify 
tanq by Rankee On June 25, 2009
i am very lucky to hav this site
Reply | Email | Modify 
thanks by Rakesh On May 28, 2010
excellent
Reply | Email | Modify 
light Box by laxman On May 31, 2010
its not working in master page please help
Reply | Email | Modify 
Great by Rathanavel On November 17, 2010
excellent
Reply | Email | Modify 
Question:How to fix the width and height in this lightbox..? by maya On March 10, 2011
:) thanks in advance
Reply | Email | Modify 
slideshow stoped. by Gangadhar On June 16, 2011
when ajax timer control is used the slide show is getting stopped when timer tick event fired. can anyone solve this problem. Thanks in advance.
Reply | Email | Modify 
Great Article by Yassar Ilyas On July 5, 2011
Thanks for this article. The code worked perfectly.
Reply | Email | Modify 
can lightbox2 read images from a folder? by Luan On November 11, 2011
I've seen that we have to manually enter each image name in the href line. Is there a way to point it to a folder so it would read all image files from there?
Reply | Email | Modify 
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.