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 »

PostBack Ritalin v1.0

AJAX, ASP.NET, PostBack Ritalin, UI By . Posted April 6, 2009

Thanks to all of your excellent feedback during the months since the last release of PostBack Ritalin, an improved version is ready for release today. With the addition of “disable all elements”, I believe the control is now feature complete, worthy of a version 1.0 release.

Notable changes in v1.0 include:

  • Added disable all elements functionality to MonitoredUpdatePanels.
  • Fixed a bug in the WaitImage preloading functionality, that prevented the MonitoredUpdatePanels’ WaitImages from being preloaded.
  • Added both debug and release versions of the JavaScript resource. Your site’s compilation mode will determine which script version is embedded.
  • Fixed a problem handling postbacks raised by Button controls that have their UseSubmitBehavior property set to false.
  • Improved ClientID handling in naming containers.

As always, you can visit the PostBack Ritalin project page to download the latest version, see usage examples, and find documentation.

$(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 »

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 »

Have you ever wondered why CodePlex is so slow?

AJAX, ASP.NET, Performance By . Posted December 3, 2008

It’s hard to believe that nearly two months have already passed since opening the doors on the Username Availability Validator project. I made a few mistakes in how I ran the project, but I’m happy with the overall outcome.

However, one thing that disappointed me throughout the entire contest was how slow CodePlex can be. Even as the single person most invested in the success of the project, I often found myself leaving the site in the middle of tasks, unwilling to endure the slowness. What’s worse, I also found myself procrastinating to avoid returning to CodePlex and dealing with these waits.

I’m not one to complain without offering some constructive criticism though. Especially since we’re talking about a completely free service.

I think the lesson to be learned here is something that many ASP.NET developers could benefit from. So, I’d like to take a look at one ASP.NET AJAX related inefficiency that I found on CodePlex and one potential alternative to it.

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 »