tags:

views:

543

answers:

3

I'm building a photo gallery in ASP.NET. The user can browse thumbnails along the left and select one, which brings a preview-sized version into the right pane of the page.

I'd like to fade between the images, so that the current one fades out and the next one fades in. I'm using jQuery to fade the preview image in after it is loaded, which works great. Unfortunately, I can't get the fadeOut script to run before the click event posts the page back to the server. The thumbnails are ASP.NET ImageButtons, which means they're <input> tags.

Is there a way to get the postback to delay just long enough for the image to fade out? I've seen some tricks with the form onSubmit and setTimeout() but that would affect all the links and buttons on the page. I want to delay postback for the thumbnails only.

TIA

EDIT: Based on my research, and trying the suggestions below, it may be possible to delay the postback to accomplish this but it's not the best approach on several levels. To get a clean fade transition between images, in the future I would not do any posting back at all. I would use jQuery exclusively for the fadeout, load, fadein.

+3  A: 

Try adding a return false to your function that handles the fadein/out... It should prevent the page postback from occurring...

$('#<%= this.aspbutton.ClientId%>').click(function(){
    $('#myDiv').fadeout("slow");
    return false;
});

I'm not sure what you are getting on the PostBack where you would want to fade out an image and then fade one in. Have you considered using AJAX for that? You could even have the thumbnail image contain the necessary information within the image tags for the larger image.

Take a look at the jQuery Lightbox plugin. I have implemented this plugin and modified the .JS a bit to allow for viewing a higher resolution photo in addition to the web view. Check it out here.

RSolberg
I think he wanted to delay the postback, not cancel it entirely. Edited to show how to do this.
Adam Lassek
that would just recurse forever
redsquare
you would need to unbind the click before invoking the click
redsquare
Unfortunately that didn't work- the postback happens before the script has a chance to fade the image. This is a usercontrol wrapped in an updatepanel, though I don't think that makes a difference...
Dave Swersky
oh late breaking news then:)
redsquare
I removed the edit that was made...
RSolberg
Hmm, you're right the click event would have to be unbound first. My bad.
Adam Lassek
A: 
$('#<%= this.aspbutton.ClientId%>').click(function(){
    var $btn = $(this);
    $('#myDiv').fadeout("slow", function() {
      $btn.unbind('click').click();
    });
    return false;
});
redsquare
Sorry, still posts back before the script gets a chance to run. I think I may need to start loading the image via AJAX through jQuery rather than posting back when the thumbnail is clicked.
Dave Swersky
It will if you have those nasty update panels involved which have hooked up events to objects inside them. See how difficult they make things! Lose them if you can and stick to jQuery. MS have abandoned them.
redsquare
If your going to stick to update panels I would close this question and start a new one that has the real facts!
redsquare
A: 

Here's the solution I used:

Since I AM using MS AJAX with an UpdatePanel, I can use the client-side AJAX event handler.

Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(fadeOut);

function fadeOut() {
        if ($('.mainImage').length > 0) {
            $('.mainImage').fadeOut('normal');
        }
    }

This gives me the exact behavior I wanted- any time the user navigates between thumbnails, the image fades out, loads, then the new one fades in.

HOWEVER...

This is still not ideal, as there is a pause between fades while the page posts back. It will work for now but in the long run it would be better to use jQuery to set the preview image rather than the thumbnails posting back as ImageButtons.

Dave Swersky