In my last post, I began a series exploring ASP.NET AJAX’s client side functionality. If you missed it, you can catch up by reading that post about the various uses of $addHandler.

In this post, I want to take a look at a few more under-appreciated tools that we’re provided with as a part of the framework: Base type extension methods.

Specifically, I’m going to provide some examples of using Array.contains, Date.format, and String.format.

Detecting UpdatePanel refreshes with Array.contains

As you may know, EndRequest’s sender._panelsToRefreshIDs contains a JavaScript array representing which UpdatePanels are being refreshed in the completing partial postback. I’ve seen a lot of code that leverages this by looping through the array and checking each element for a particular ClientID. However, ASP.NET AJAX’s extensions to the base JavaScript Array type provides us with a more elegant way of doing this:

Additionally, the Array type extensions also includes methods such as Array.add, Array.insert, Array.remove, and more. The ASP.NET AJAX documentation contains information on them all.

Simplify formatting client side dates with Date.format

If you’ve spent much time working with dates in JavaScript, you know what a hassle it can sometimes be. To greatly ease this, ASP.NET AJAX extends the JavaScript Date object with a formatting function that closely mimics the ToString formatting paradigm that we’ve been using for years. Here are a few examples:

Date.format accepts most of the standard DateTime format strings, such as d and D. If none of those suit your needs, Date.format also provides almost unlimited flexibility via custom formatting strings. For a full list of formatting string parameters, see the Standard DateTime Format Strings and Custom DateTime Format Strings reference pages on MSDN.

String plus Number, divided by Date?!

JavaScript is a weakly typed language. That is to say, a given variable’s type is only known at runtime. Since string concatenation and mathematical addition are both accomplished through the same operator, this can cause a bit of confusion. For example:

Among the base type extensions provided by ASP.NET AJAX, a static format method has been added to the String class. Like Date.format, String.format works almost identically like its namesake method in the .NET framework:

String.format also accepts the formatting strings supported by the base type of the substitution variable. For example:

Even if you set aside the type inference issues, I find it extremely useful to be able to format my strings with almost identical code on both client and server side. Hopefully, you’ll find it helpful too.


As you can see, ASP.NET AJAX improves quite a few aspects of JavaScript’s base types. If these few examples have whetted your appetite for more, make sure you take a look at the JavaScript Base Type Extensions documentation.

It’s worth noting that just because the framework provides an extension method for a task, that doesn’t mean it’s the best way to accomplish the task. Consider Array.forEach, for example. I’m really not sure what the intended benefit of Array.forEach is, but a regular JavaScript for-loop is easier to use and more readable in every scenario I can think of.

In other words, while these extension methods are tremendously useful, they aren’t an excuse to avoid familiarizing yourself with traditional JavaScript development.