How I handle JSON dates returned by ASP.NET AJAX

AJAX, ASP.NET, JavaScript, jQuery By . Updated May 1, 2009

A calendar

The problem of how to handle dates in JSON is one of the more troublesome issues that may arise when directly calling ASP.NET AJAX web services and page methods.

Unlike every other data type in the language, JavaScript offers no declarative method for expressing a Date. Consequently, embedding them within JSON requires a bit of fancy footwork. Since the question of how I handle this problem is something asked often in emails and in comments on other posts here, I want to address the topic with its own post.

To that end, I will attempt to explain what exactly the problem is with dates in JSON, how ASP.NET AJAX solves it, and my alternative solution that I believe is easier and works just as well in most cases.

Click here to read the rest of this post »

Using complex types to make calling services less… complex

AJAX, ASP.NET, JavaScript, jQuery By . Updated June 6, 2009

So far, my examples of using jQuery to interact with ASP.NET AJAX services have avoided passing complex data to the server during the request. This has been intentional, because I didn’t want to over-complicate the examples.

For primarily read-only scenarios, like the RSS reader examples, passing just a few simple values to the service is often all you need. However, this scalar approach quickly becomes untenable when making real-world service calls.

In this post, I’m going to show you how passing complex types to the server helps alleviate complexity, how json2.js and a data transfer object (DTO) facilitates this, and how to use jQuery to very easily build the DTO.

Click here to read the rest of this post »

$(document).ready() and pageLoad() are not the same!

AJAX, ASP.NET, JavaScript, jQuery By . Posted March 25, 2009

Recently, I’ve attended several presentations in which ASP.NET AJAX’s pageLoad() shortcut is demonstrated as interchangeable with jQuery’s $(document).ready() event. The suggestion that both methods are equivalent actually appears to be true in simple demos, but is not the case and is certain to lead to later confusion.

While they seem similar on the surface, $(document).ready() and pageLoad() are very different behind the scenes. Determining the earliest point that it’s safe to modify the DOM requires a bit of black magic, and the two libraries approach that in their own unique ways. Additionally, pageLoad() is overloaded with some extra functionality which may surprise you.

In this post, I’ll clarify the major differences between jQuery and ASP.NET AJAX’s initialization functions, what implications those difference have in practice, and show you a third alternative when working with ASP.NET AJAX.

Click here to read the rest of this post »

Use jQuery to catch and display ASP.NET AJAX service errors

AJAX, ASP.NET, jQuery, UI By . Posted March 4, 2009

Another user fed up with your lack of error handling!

If you don’t properly handle the inevitable errors in your web applications, you can expect your users to eventually react about like this guy. Since they typically squelch any server-side errors, AJAX service calls are especially problematic. In fact, they rarely even throw a client-side error when they fail.

Even when a client-side error is thrown, most users won’t notice it and the ones who do notice won’t know what the error means or what to do next. In fact, I’ve found that even many developers don’t notice client-side scripting errors that occur while they’re debugging their own applications!

To help you remedy this problem in your own applications, I want to show you one way that I handle AJAX service call errors with jQuery. To do this, we will build an error-prone web service, make an AJAX request to it via jQuery, handle the resulting server-side errors gracefully, and use a jQuery plugin to attractively present those errors.

Click here to read the rest of this post »

A breaking change between versions of ASP.NET AJAX

AJAX, ASP.NET, JavaScript, jQuery By . Posted February 10, 2009

When working directly with JSON serialized ASMX services, be it via jQuery, pure XmlHttpRequest calls, or anything else other than the ScriptManager, one question inevitably arises. That question is of the inexplicable .d attribute that appeared in ASP.NET 3.5.

What is it? Why is it there?

In this post, I’ll use both a 2.0 and a 3.5 example ASMX web service to illustrate exactly what’s going on. I’ll also show you why it’s a good change.

Click here to read the rest of this post »

3 reasons why you should let Google host jQuery for you

AJAX, JavaScript, jQuery, Performance By . Updated February 17, 2014

All too often, I find code similar to this when inspecting the source for public websites that use jQuery:

<script type="text/javascript" src="/js/jQuery.min.js"></script>

If you’re doing this on a public facing website, you are doing it wrong.

Instead, I urge you to use the Google HostedLibraries content delivery network to serve jQuery to your users directly from Google’s network of datacenters. Doing so has several advantages over hosting jQuery on your server(s): decreased latency, increased parallelism, and better caching.

In this post, I will expand upon those three benefits of Google’s CDN and show you a couple examples of how you can make use of the service.

Update: Since you’re reading this post, you may also be interested to know that Google also hosts full jQuery UI themes on the AJAX APIs CDN.

If you’ve already read all this and are just here for the link, here it is:Note: the “missing” http: in that URL isn’t a mistake. You can learn more about that later in this post.

Click here to read the rest of this post »

Use jQuery and quickSearch to interactively search any data

AJAX, ASP.NET, jQuery, UI By . Updated October 16, 2013

While reviewing my latest post’s outbound click-through stats, I noticed that most of you found Rik LomasquickSearch more interesting than the rest. In light of that interest, I’ve decided to show you an example of how you might implement its functionality in conjunction with ASP.NET.

For this demonstration, I’m going to use Delicious as a source of sample data to apply the technique to. I’m sure you’re getting tired of my RSS feed as demo data, and the Delicious API provides an abundance of topical data via jQuery tag feeds.

In this post I will show you how to use LINQ to XML to query the Delicious API, display that data in a table, apply the quickSearch plugin to that table, work around a problem that arises when using quickSearch with ASP.NET, and lay the groundwork for a “no results” state.

Click here to read the rest of this post »

7 of my favorite jQuery plugins for use with ASP.NET

AJAX, ASP.NET, jQuery, UI By . Updated January 26, 2010

One of jQuery’s greatest strengths is its thriving plugin ecosystem. Hundreds of plugins are available at plugins.jquery.com alone, with even more hosted on author sites. If you think of a feature, chances are there’s a jQuery plugin to implement it in just a few lines of code (if that).

Having used jQuery with ASP.NET for over a year now, I’ve discovered that some plugins are more easily combined with ASP.NET than others. In fact, one popular plugin even fails completely when used with ASP.NET AJAX.

Without further ado, here are seven of my favorites. I couldn’t begin to rank them preferentially, so I ordered them by how long I’ve been using them with ASP.NET.

Click here to read the rest of this post »

Using jQuery to display a modal UpdatePanel confirmation

AJAX, ASP.NET, jQuery, UI By . Updated October 15, 2008

A screenshot of the modal confirmation dialogAfter the previous example of integrating jQuery and ASP.NET AJAX to display modal progress indication, there were requests that I follow it up with an example of also displaying the confirmation modally. So, I’m going to expand the original post with a technique for doing exactly that.

If you missed my first post on the topic, do be sure to read it first. Otherwise, this continuation may be difficult to follow.

Click here to read the rest of this post »

Alt.NET Podcast – jQuery in ASP.NET

AJAX, ASP.NET, JavaScript, jQuery By . Updated October 18, 2008

If you’re reading this, you probably already know that I’m a big fan of integrating jQuery with ASP.NET. If you’re of the same mind, you’re sure to enjoy the latest Alt.NET Podcast: jQuery in ASP.NET.

Update: Part two of our discussion on jQuery and ASP.NET.

Mike Moore hosted a roundtable discussion on the topic, between Bertrand Le Roy, Chris Brandsma, Rick Strahl, Steven Harman, Scott Koon, and myself. Between us all, the discussion ranges from the official Microsoft viewpoint, to theory about JavaScript and jQuery, to the pragmatic.

Whether you’re already on board with jQuery + ASP.NET integration or you’re still trying to figure out what the announcement means for you, I think you’ll find the discussion interesting. I know I did!