LightBox with ASP.NET
If you don't know what LightBoxes are check out the list at
blinklist. Among them two implementantions caught my attention, the original
LightBox JS 2.0 by Lokesh Dhakar and the lighter
LiteBox by Tyler Mulligan.
The final result is exactly the same, they are able to show images in a really nice fashion, as well as create a slideshow out of them. They differ in some aspects by the way:
- LightBox JS is based on script.aculo.us and prototype while LiteBox is based on moo.fx and prototype.lite, which are pratically a subset of the original libraries.
- LightBox JS is heavy since its weight is about 100k, while LiteBox is much lighter, about 30k.
You might be wondering why the effect is exacly the same; well, script.aculo.us and prototype espose a great amount of functionality which LightBox JS doesn't exploit, so there's no need for such heavy libraries. That's why I chose LiteBox for my own port to ASP.NET.
Ok, so we want to get that effect with our images. In order to set LiteBox up you need to:
- include the scripts into your page
- import a stylesheet
- make LiteBox images available in some folder of your website in order for it to be able to retrieve them (I mean the images which show up in the LiteBox to perform navigation and close the box)
- place your anchors pointing to your images on the page
- set the rel attribute of your anchor elements to "lightbox"
- [optional] or set the rel attribute to "lightbox[group]" in order to group together a set of images so that you can surf through them
- [optional] set the title attribute of the anchors to show a caption for the image.
Ok, too much to do in my opinion. Not very reusable. Let alone that if you want to show thumbnails of the images you have to wrap your anchors around
img tags! (ASP.NET HyperLink controls instead expose a property called ImageUrl which does just the same, and it's what I've used for my implementation).
So here comes my wrapper.
Nothing to read here the control LiteBoxExtender is obsolete, read on. I created a webcontrol called LiteBoxExtender which includes the scripts and the stylesheet for you, and which will come even more useful later. This control is a non visual control, and works together with another control called LiteBoxImageLink which is wired up automatically to the LiteBoxExtender once dragged on the webform and which represents a link to an image. The
LiteBoxImageLink is derived from
HyperLink and behaves the same it does. It only exposes one more property called Group.
So in order to set it up all you have to do is:
- Drag on the page as many LiteBoxImageLinks as you want. Each one will represent an image which will show up in the LiteBox.
- Set the properties of the LiteBoxImageLink controls:
- NavigateUrl: the url of the image which will show up in the LiteBox
- [optional] Text: the text to show in the link to the image
- [optional] ImageUrl: the url of the preview image (to be used instead of Text)
- [optional] Group: a group name, in order to group images together and be able to surf through them.
It's that easy, no more scripts and styles to deal with, no images to copy in your website folders, and no manual editing of your anchors to wire up the LiteBox, the controls will do that by themselves. Of course it will work with normal HyperLink controls too as well as with anything that renders on the page as an html anchor tag, but in that case you'll have to follow the manual procedure and set the rel attributes yourself.
The control is available inside my Script.Aculo.Us.Net library.
UPDATE: sources are now only available via the SVN repository. Read here for details. If you download the library you'll find another control called LiteBoxAlbum. I'm not talking about it here since it deserves another post, but it is much cooler than the controls I've described in this post, so you may want to check it out. One advice only, create a blank webform, drag a LiteBox album control in it and place some images (.jpg or .gif) in the same directory as the webform. Then just browse to the page and enjoy!
UPDATE: The follow-up has been posted and is available here.