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 »

Review: The best JavaScript book I’ve read

JavaScript, Reading By . Updated March 1, 2009

Having used JavaScript for over a decade, I’ve read many books covering the language. Some focused primarily on syntax. Others recounted and solved specific real-world problems.

Learning a language as a set of tasks is one way to get up to speed quickly, but it’s not a very good way to thoroughly learn a language’s nuances and idioms.

While those sorts of books certainly have their place, it’s disappointingly rare to find a book which presents JavaScript as the first-class programming language that it truly is.

After reading no more than the first page of this book’s preface, I knew that I had finally found the antidote to those trite examples of compound interest calculators and the tedious minutiae of books that spend pages on alert()’s syntax. No, this book is different than the rest…

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 »

How to rotate JavaScript ads during ASP.NET AJAX requests

AJAX, ASP.NET, JavaScript, UI By . Updated January 8, 2009

A common question that I find myself fielding lately is how to handle the problem of rotating JavaScript based advertisements. As AJAX becomes ubiquitous on ad supported sites, we must take care that decreasing page views don’t also mean proportionally decreasing revenues.

Many find their way to ScriptManager.RegisterStartupScript, use that to re-inject the script during a partial postback, and expect that doing so will refresh the ad. Unfortunately, that doesn’t work when dealing with JavaScript based ads such as Google’s AdSense.

In this post, I’m going to show you why RegisterStartupScript does not solve the problem, the alternative that I suggest, and how to use that method to also defer loading of advertisements until after your content.

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 March 1, 2013

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 AJAX Libraries 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 »

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!

Review: Advanced ASP.NET AJAX Server Controls

AJAX, ASP.NET, JavaScript, Reading By . Updated September 4, 2008

The most common complaint I typically have with the ASP.NET AJAX books I review is that they simply don’t cover JavaScript in enough detail. JavaScript represents half of the nouns in the AJAX acronym, and all of those relevant to ASP.NET AJAX.

For that reason, when I read a book that only contains a few pages about remedial JavaScript and then jumps directly into using UpdatePanels and Timers, I don’t recommend it to you.

However, I was very impressed when I began reading Adam Calderon and Joel Rumerman‘s recent book, Advanced ASP.NET AJAX Server Controls.

Click here to read the rest of this post »

Easily build powerful client-side AJAX paging, using jQuery

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

A book's pages blowing in the wind

When I suggest that developers consider using web services and a more client-centric approach to solve their UpdatePanel performance problems, the lack of paging is often their first objection.

Conventional ASP.NET wisdom seems to hold that the GridView/UpdatePanel combo is king when asynchronously paging through a data source. If you’ll give me a few minutes of your time, I’d like to challenge that notion!

Click here to read the rest of this post »