On the road again...

The life of a .NET commuter.

jQuery, Facebox and AJAX

One frustration that I've had for the 4 years that I've been working with AJAX is how messy the front-side code can get. There's lots of JavaScript, and that can get kind of annoying. Plus, there's the design perspective of trying to find the right real-estate to put things in. Where do you have a hidden div pop up and provide space for an update? I've been working on a project for my wife, and I've found a nice way to get around some of that, separate my code a little bit, and clean things up so they aren't so cluttered.

Recently, I ran across the jQuery library. It is a seemingly robust JavaScript wrapper that simplifies (greatly) the amount of code you write for a routine task. For example, before jQuery, if you wanted to toggle the visibility (display) of a div, you would have to do this:

function toggle(p)

{

if (document.getElementById("case" + p + "List").style.display == "none")

{

document.getElementById("case" + p + "List").style.display = "inline";

document.getElementById("img" + p).src = 'images/minus.jpg';

}

else

{

document.getElementById("case" + p + "List").style.display = "none";

document.getElementById("img" + p).src = 'images/plus.jpg';

}

}

With jQuery, you simply do this:

function toggleStatus()

{

$("#statusChange").toggle();

}

It's really that simple. So, jQuery is a great little framework that allows me to do a ton of stuff.

Enter "Facebox." This is a jQuery plug-in that creates a Facebook style pop-up window. It can be used as a lightbox, or more importantly for me, as a container for other content, including complete pages. To open an instance of Facebox, all I do is create a hyperlink tag (<a href='test.htm' rel='facebook'>test</a>). When I click this link, Facebox pops up with the contents of test.htm neatly packed inside. Beautiful.

Now, I simply create a new page, call it "createnewwhatever.aspx," plug it in to my URL, and do all my AJAX stuff on that page. Keeps it clean, because it's not on your main page, and it's reusable.

The one thing Facebox cannot do is post-backs. That doesn't really bother me much yet. Who knows if it will or not. I've also not had much luck with jQuery and MasterPages. (Facebox works, but I'm having trouble with other plugs.) I probably just don't understand the syntax well enough.

But, this represents a significant step forward for me in terms of UI design and usability. Try it out.

Posted: Jan 22 2008, 03:06 PM by du8die | with 12 comment(s) |
Filed under: , ,

Comments

rrobbins said:

Yes, I recently discovered how useful jQuery can be. I just used it to give alternating table rows a class so that my CSS can style a GridView. Come to think of it, why didn't I use an AlternatingRowStyle CssClass? Doh!

$(document).ready(function(){

$("tr:odd").addClass("odd");

});

# January 22, 2008 3:59 PM

John S. said:

What issues are you having with jQuery and MasterPages? I'm using those two together without issue (other than ASP.NET's IContainer auto-generated IDs).

# January 22, 2008 4:34 PM

du8die said:

Hey John,

The problem is probably more with me, and I don't understand how nesting and such works in jQuery.  I figured out a few hacks that seem to do the trick.  I should have been more specific.  I'm having more trouble with the plugs on MasterPages...  

Will get there...

# January 22, 2008 4:43 PM

Facebook » jQuery, Facebox and AJAX said:

Pingback from  Facebook &raquo; jQuery, Facebox and AJAX

# January 22, 2008 4:55 PM

James said:

JQuery is excellent.  Using the selectors with asp.net controls can be difficult as asp.net auto generates new client side ids.  I've gotten around that by using non .net controls, adding a class to the .net control, or writing some code to pass the generated id to the javascript code.

# January 22, 2008 5:51 PM

du8die said:

$("#<%=myControl.ClientId%>") works pretty well.

# January 22, 2008 7:09 PM

Trumpi's blog said:

jQuery MooTools 1.2 Beta 2 Released, Adds New Element Storage jQuery, Facebox and AJAX Agile The magic

# January 23, 2008 2:02 PM

On the road again... said:

A few days ago, I posted about getting started with AJAX, Facebox and jQuery . Well, I've been doing

# January 25, 2008 11:08 PM

Jim said:

If you going to use master (asp.net), strip ALL web controls and use generic html form fields, basically do traditonal web development code using .net. ASP.NET sucks, but .NET C# is great for all processing.

# February 1, 2008 2:04 PM

Vanessa said:

Trying to get the validation text into facebox after i have send data through it.

Cant figure it out. Is this want you meant by facebox cannot do postbacks?

# February 29, 2008 10:05 PM

brad said:

I too have been using facebox with great success ... until using it in conjunction with SIFR. For some reason, the SIFR ignores the z-index set on the div with the rel= facebox, and always is above it.(even though it has a z-index of 1 and the facebox 100) I am at my wits end. i have no idea what else to do ... I have tried everythingggggggg.

anybody in the same boat as me?

# March 5, 2008 4:28 AM

vipergtsrz@gmail.com said:

I am currently working with facebox and jquery, and I also noticed that it doesn't work with postbacks. I took a quick look at the code and if you check it out, it has a line here that tells it to append to the body tag.

$('body').append($.facebox.settings.faceboxHtml)

If you just replace the text 'body' with 'form', then it will append the facebox div to the form tag instead which will allow postbacks. If it appends to the body tag, it will be outside the form and won't work.

Hope that helps.

# June 3, 2009 10:12 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)