Monday, July 07, 2008 1:08 PM djsolid

Create a Facebook-like Ajax Image Gallery!

A lot of people have been using facebook and i am sure that everyone has noticed the ajax gallery that faecebook has. In this article we will try to make our own gallery that does the same stuff. When the user clicks on the image he navigates to the next and when the user presses the back button of the browser the previous image is displayed.

We are going to use a new feature of the AJAX Framework which is History Support! (available via the property of ScriptManager, EnableHistory)

First create a new "ASP.NET 3.5 Extensions Web Site"

Then at the aspx page we add :

2Capture

EnableHistory : Enables the new feature of AJAX Framework which allows the user to navigate back and forward in an AJAX page.

EnableStateHash : If true the url responible for AJAX History is encoded. By Default is true.

OnNavigate : This event is fired when the user clicks the back button of the browser and the ScriptManager makes a postback.

At the aspx.cs page we add :

Capture

First we read the image folder (it could be from a database). We strip out the full path and we keep only the image name. Then we check if this is not a postback and we display the first image of our list. When the user clicks the Back Button the Navigate Event of the ScriptManager is fired. We get the current image index and we add +1.

1Capture

When the user clicks on the ImageButton the Click event is fired. The point of interest at this method is the AddHistoryPoint of the ScriptManager. Using this method we add something that can tell us (when the user clicks the back button) what was the page like in order to be able to recreate it.

 

You can download the source code from here!

 

You can view a live example here!

That's it! enJoy!

Filed under: , , , , ,

Comments

# ASP.NET AJAX History and ASP.NET Hacks

Monday, July 07, 2008 12:30 PM by Denny.NET

ASP.NET AJAX History and ASP.NET Hacks

# re: Create a Facebook-like Ajax Image Gallery!

Tuesday, July 08, 2008 3:35 AM by nmattheij

Your live example throws an ASP.NET exception.

# re: Create a Facebook-like Ajax Image Gallery!

Tuesday, July 08, 2008 8:46 AM by djsolid

Really? I just checked and every thing works fine. Is it possible to be more specific please?

Thanks,

Johh

# re: Create a Facebook-like Ajax Image Gallery!

Wednesday, July 29, 2009 3:11 AM by name

I want to say thanks!,

# re: Create a Facebook-like Ajax Image Gallery!

Thursday, July 30, 2009 12:07 AM by name

Where it is possible to buy the,

# re: Create a Facebook-like Ajax Image Gallery!

Tuesday, October 13, 2009 6:11 PM by great artcle!

great artcle!

# re: Create a Facebook-like Ajax Image Gallery!

Wednesday, November 18, 2009 6:30 AM by Elikat

I get the exception Sequence contains no elements

on this line:

string ImgName = l.Where(x => x.FileName == GalleryImageButton.ImageUrl.Substring(GalleryImageButton.ImageUrl.LastIndexOf("/") + 1)).Select(x=>x.FileName.Substring(x.FileName.LastIndexOf("\\")+1)).First();

Kindly assist.

# re: Create a Facebook-like Ajax Image Gallery!

Wednesday, November 18, 2009 6:51 AM by Elikat

Another exception on this line:

 l = (List<ImageInfo>)Cache["GalleryImages"];

-------

[A]System.Collections.Generic.List`1[Gallery+ImageInfo] cannot be cast to [B]System.Collections.Generic.List`1[Gallery+ImageInfo]. Type A originates from 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll'. Type B originates from 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll'.

Leave a Comment

(required) 
(required) 
(optional)
(required)