Sunday, October 07, 2007


Validators Lost

An obvious ASP.NET 2.0 wart is its poor documentation of validators. As previously noted, there is a March, 2002, background article for ASP.NET 1.0 validators, no longer distributed with MSDN. However, it has not been updated since 2002, and it fails to explain many validator behaviors.

For example, when you enclose validators in a DIV that is initially hidden, you will find they don't work. That is because ASP.NET 2.0 conveniently disables them for you. Validators undergo preprocessing by the JavaScript that ASP.NET 2.0 emits, at the time a page loads in a browser. Hidden validators are disabled then; ASP.NET designers apparently thought that would be helpful. However, if you make a DIV visible, they didn't follow through by enabling validators it may enclose.

To remedy the deficiency, enable validators in JavaScript. ASP.NET 2.0 provides a global JavaScript function to do that, invoked in the following example:

    var oValidator =
     document.getElementById('<~% = ofValidator.ClientID %~>');
    if (oValidator != null)
        ValidatorEnable(oValidator, true);
        if ( != 'none')
    = 'hidden';

A wrinkle, as the example suggests, is that the Text message for a validator will be conveniently made visible when the validator is enabled. If that is not so convenient, then hide the message as shown.

[Note: because of display limitations, characters "< " and " >" here are shown with a tilde ~ after or before them.]

Comments: Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]