Work smarter: MS AJAX’s JavaScript type extensions
AJAX, ASP.NET, JavaScript By Dave Ward. Updated September 28, 2008
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:
function EndRequest(sender, args) { // This will do something, only if UpdatePanel1 is being refreshed. if (Array.contains(sender._panelsToRefreshIDs, 'UpdatePanel1')) DoSomething(); }
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:
var today = new Date('12/3/2007'); var shortDate = today.format('d'); // d formats the date as MM/dd/yyyy // shortDate == '12/03/2007' var longDate = today.format('D'); // D formats the date as dddd, dd MMMM yyyy // longDate == 'Monday, 03 December 2007' var customDate = today.format('MMMM, yyyy'); // Custom format string to format the date as MMMM, yyyy // customDate == 'December, 2007'
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:
var a = 11; b = 22; c = '33'; alert(a + b + c); // Due to type inference, a + b + c == 33 + '33' == '3333', // not the '112233' you might expect.
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:
var a = 11; b = 22; c = '33'; alert(String.format('{0}{1}{2}', a, b, c)); // As desired, the alert will now be '112233'.
String.format also accepts the formatting strings supported by the base type of the substitution variable. For example:
var result = String.format('Date: {0:d}', new Date('12/3/2007')); // result == 'Date: Monday, 03 December 2007'
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.
Conclusion
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.
Similar posts
What do you think?
I appreciate all of your comments, but please try to stay on topic. If you have a question unrelated to this post, I recommend posting on the ASP.NET forums or Stack Overflow instead.
If you're replying to another comment, use the threading feature by clicking "Reply to this comment" before submitting your own.
6 Mentions Elsewhere
- Links del 8 de Diciembre: ASP.NET, ASP.NET AJAX, ASP.NET MVC, .NET, VS 2008 « Thinking in .NET
- Pawel Klimczyk WebLog » Blog Archive » Visual Studio 2008 developers links !
- HowTo: Microsoft AJAX Client API nutzen | Code-Inside Blog
- December 8th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, .NET, VS 2008 « .NET Framework tips
- Jan 24th Links: ASP.NET, ASP.NET AJAX, Visual Studio, .NET, IIS - ScottGu's Blog
- AJAX links « bnotezz



How could you implement a foreach loop in javascript without Array.forEach()? It only has a regular for loop, doesn’t it?
The JavaScript “for” statement is overloaded with a lesser known syntax that functions much like a foreach statement. For example:
It also works on associative arrays, which makes it very handy.
Thanks Dave,
Doing a great work !!!!
I find very small number of articles, that focus on small but important things,
Example,
this article, “Why Update Panel are dangerous”, “Improving AJAX Error handling” etc..
Please keep it up..
Good post Dave,
The JS extensions are indeed awesome!
Nice work, short but very usefull post.
question re: javascript
Does it respect order of operations in this case?
c + b + a should equal ’332211′ right?
’33′ + 22 = ’3322′ + 11 = ’332211′
Not that this is a solution to the problem, but if you start with a string, it will always end with one, correct?
Yes, that’s correct.