The jQuery Globalization plugin becomes “Globalize”

The jQuery Globalization plugin originally developed by the ASP.NET team and donated to the jQuery project, has found new life as the “Globalize” JS library.

This new library is maintained by the jQuery UI team but no longer has a dependency on jQuery. It is completely standalone, meaning you can use it with any JS library or environment you like. That’s very cool.

Moving forward, the jQuery UI library will utilize Globalize for providing globalization of the its widgets, and expose an API interface that can be met by Globalize while encouraging widget authors to utilize this API to ensure any widget can be globalized in the same way, with or without globalize.js as a depedency. That’s cool too.

The library includes culture information for ~350 cultures. That’s quite a lot. And an interesting fact: these culture files are generated from the culture info in the .NET framework. That’s especially cool.

You can grab globalize.js and the desired culture files from its home on the jQuery github account. It’s currently in “in development” status at version 0.1.0a1. You can read more about plans for its development and further releases on its jQuery UI wiki page.


Access Key Highlighter plugin for jQuery now available

I’ve just published my first jQuery plugin release. It’s a port of my Access Key Highlighter control for ASP.NET AJAX that I released last year. I’ve rewritten the control for jQuery and plan to implement new features in both versions of the control from now on. You can see a live demo of it running at http://files.damianedwards.com/KeyTips/

Access Key Highlighter popups on form field labelsAccess Key Highlighter popups on buttons and hyperlinks

 

If you have any feedback or feature suggestions go ahead and leave a comment here.


Setting up jQuery for ASP.NET Web Forms projects

During my recent presentation to the Victoria.NET DevSIG on jQuery, I talked about how to get started using jQuery with ASP.NET Web Forms projects. Part of this was around how I like to set up my projects with jQuery to take advantage of the script management features that ASP.NET & Visual Studio 2008 provide out of the box.

ASP.NET provides support for switching in different versions of your JavaScript files at runtime depending on the compilation setting in your web.config file (debug=true or false) via the ScriptManager control. This allows you to have your development script used during development & debug time, and an optimised script (minimised, obfuscated, etc.) used at release time. You can enable this feature of the ScriptManager with your own files (it does it automatically for the MS AJAX framework files) in a few ways, the easiest of which is to set the ScriptMode property of your ScriptReferences to Inherit.

<asp:ScriptManager runat="server">
     <Scripts>
         <asp:ScriptReference Path="~/script/myScript.js" ScriptMode="Inherit" />
     </Scripts>
</asp:ScriptManager>

Further to this, Visual Studio 2008, by way of a hot fix, adds support for a third type of JavaScript file, used only for providing JavaScript IntelliSense within the Visual Studio IDE. These special versions of your script files (known as “VSDoc” files) are carefully constructed to ensure optimal IntelliSense relevance, information & performance and generally are not able to be used at runtime at all.

So we have three file types in all as follows:

  • Release mode file: myscript.js
  • Debug mode file: myscript.debug.js
  • VSDoc file: myscript-vsdoc.js

When you download jQuery, you have the option of the standard file (jquery-1.3.2.js at time of writing) as well as the “production” file, which is minimised (jquery-1.3.2.min.js). Microsoft have also contributed a VSDoc file which you can download from the official jQuery source repository on Google Code.

So there are three types of jQuery files that match up with what ASP.NET & Visual Studio support, just two of them have the wrong extension. All we need to do is rename the files to match the features in the platform & tools:

  • jquery-1.3.2.js => jquery-1.3.2.debug.js
  • jquery-1.3.2.min.js => jquery-1.3.2.js

I have this files in a location on my hard drive where I can always get to them for new projects:

image

To use them in a project, just create a folder to hold them and right-click in solution explorer and choose Add –> Existing Item…

image

Now simply add a script reference for the jQuery files in the same way as you would for your own script. ASP.NET will use the large debug version when the app is in debug mode & the minimised version when it isn’t, plus you’ll get great IntelliSense support from Visual Studio 2008:

<asp:ScriptManager runat="server">
     <Scripts>
         <asp:ScriptReference Path="~/script/jquery-1.3.2.js" ScriptMode="Inherit" />
         <asp:ScriptReference Path="~/script/myScript.js" ScriptMode="Inherit" />
     </Scripts>
</asp:ScriptManager>

It would be nice if the next version of ASP.NET included support for jQuery’s default file extensions in the ScriptManager control, but until then this works very well.


Set the CSS class of ASP.NET invalid controls using jQuery

The ASP.NET validation controls are a very quick and effective way to add client-side & server-side validation to your web forms but they lack one very important feature out of the box: the ability to change the CSS class of the controls that are invalid. Changing the class of the invalid controls will allow you to style them in such a way that they help bring attention to the fact that the user needs to do something more to complete the form successfully.

It is quite simple to add this functionality (for clients with JavaScript enabled at least) using a little bit of JavaScript and everybody’s favourite JavaScript library, jQuery!

First of all, set the CssClass property of all your validator controls to “validation-error”. The easiest way to achieve this is by using a skin file. See this post for more details on how to do this.

Then add the following to your site’s JavaScript file (assuming you’re already using jQuery):

 
/// <reference path="jquery-1.3.1.js" />

$(document).ready(function(e) {
    $("span.validation-error")
        .bind("DOMAttrModified propertychange", function(e) {
            // Exit early if IE because it throws this event lots more
            if (e.originalEvent.propertyName && e.originalEvent.propertyName != "isvalid") return;

            var controlToValidate = $("#" + this.controltovalidate);
            var validators = controlToValidate.attr("Validators");
            if (validators == null) return;

            var isValid = true;
            $(validators).each(function() {
                if (this.isvalid !== true) {
                    isValid = false;
                }
            });

            if (isValid) {
                controlToValidate.removeClass("error");
            } else {
                controlToValidate.addClass("error");
            }
        });
});

And there you have it. Now when any validator on your form has the isValid property tripped to false on the client side, the CSS class of the offending control will be changed to “error”.


Speaking at VIC.NET SIG next week on jQuery

I’ll be speaking at the Victoria.NET SIG meeting next week in Melbourne about jQuery. Come along if you want to learn about what jQuery is and how it can help you in your ASP.NET applications. RSVP details below.

 

jQuery: The way JavaScript should be

Thursday 29th January 2009
5:30pm (Pizza and drinks served); 6pm (presentations start)
Microsoft Theatre, Level 5, 4 Freshwater Place, Southbank

Hate the thought of writing JavaScript? Get frustrated by cross-browser DOM quirks? Ever wondered how those spiffy sites do that animation without Flash or Silverlight? Think that JavaScript isn’t a real programming language? Well come and see how jQuery makes writing JavaScript fun again. Microsoft is now shipping and supporting jQuery, an open-source JavaScript library, with ASP.NET and Visual Studio. jQuery is fast, lean, simple and hugely expandable, to enable you to build the most compelling web applications quickly and easily.

About the speaker:

Damian Edwards has presented at several Usergroup meetings, community events and Tech Ed on a wide range of topics. He is also a Microsoft MVP who specialises in ASP .NET and Web Front end development.

If you are planning on coming, please RSVP to help us organise drinks and catering.

To register, simply send an email to info@victoriadotnet.com.au. It is a FREE event and there is no charge for attendance but space is limited so you’ll need to let us know you’re coming so we can make sure there’s space. If you do not wish to receive event notification mails in the future, simply send a mail to info@victoriadotnet.com.au with the word Unsubscribe in the subject.

Twitter Account and other SIGs:

The user group has a Twitter account. You can follow the happenings in the group at http://twitter.com/vdnug

The Victoria .NET Dev SIG usually meets on the second Tuesday of every month to discuss developer related .NET topics. Victoria .NET also runs an Office and SharePoint group called MOSSIG (that meets on the 4th Wednesday of every month) and a SQL SIG (that meets 12:30-2:00pm on the 2nd Monday of each month) to discuss topics related to SQL Server development and a new Silverlight Developer and Designer Network user group