Monday, June 04, 2007

 

Button validation and more

ASP.NET 2.0 introduced the ValidationGroup property associating validators with action controls. It is often used to make asp:Button controls activate a designated set of validators before a postback. Sometimes buttons also need to trigger other client-side work before a postback. For such purposes ASP.NET 2.0 introduced the OnClientClick property of asp:Button, specifying JavaScript to be executed before postback, which can return a boolean value to control whether or not postback occurs.

Useful features, except when you need them both. If you try that, you will find that your OnClientClick code runs but your validators do not. Microsoft does not give advice on how to handle this situation or even tell you that it happens. What is going on here is that the HTML produced to represent an asp:Button with validators has an onclick property specifying a function that runs the validators in the associated ValidationGroup. The OnClientClick property of asp:Button overrides that onclick HTML property.

You get both features to work together by activating the validators inside the JavaScript code specified by OnClientClick, using undocumented variables and functions as shown in the following partial example:


<~asp:Button ID="ofButtonTest" runat="server"
 OnClientClick="return ValidateAndDoWork('Submit')"
 OnClick="ButtonTest_Click" ValidationGroup="Submit" ... /~>


function ValidateAndDoWork(oValidationGroup)
{
    if (Page_Validators == null || ValidatorValidate == null
     || ValidatorUpdateIsValid == null || Page_BlockSubmit == null
     || ValidationSummaryOnSubmit == null || Page_IsValid == null
     || __defaultFired == null)
    {
        DoWork();
        return true;
    }
    var oValidatorCount = Page_Validators.length;
    for (var oValidatorIndex = 0;
     oValidatorIndex < oValidatorCount; ++oValidatorIndex)
        ValidatorValidate(Page_Validators[oValidatorIndex],
         oValidationGroup, null);
    ValidatorUpdateIsValid();
    ValidationSummaryOnSubmit(oValidationGroup);
    Page_BlockSubmit = !Page_IsValid;
    if (Page_IsValid)
        DoWork();
    else
        __defaultFired = false;
    return Page_IsValid;
}


function DoWork()
{
    ...
}


The ValidateAndDoWork function first executes all validators for a specified validation group. Note that the JavaScript specified for OnClientClick must return a value and must know, determine or receive as a function argument the name of the associated ValidationGroup. If validation passes, ValidateAndDoWork invokes DoWork to perform other client-side work before a postback and returns true. Otherwise ValidateAndDoWork returns false, preventing a postback.

ASP.NET client-side validation does not operate under FireFox, Mozilla and Netscape. Thanks to Tom Newman, posting on Scott Guthrie's Web log at http://weblogs.asp.net/scottgu/archive/2005/08/04/421647.aspx, who illustrated undocumented client-side variables and functions that work under ASP.NET 2.0 and are used here.

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

----------------------------------------------------------------------

Update, August 12, 2007

Page_Validators, ValidatorValidate, etc. are not entirely undocumented. Anthony Moore, Microsoft's Development Lead for .NET Framework and later for Base Class Libraries, wrote a helpful article on validation describing these elements at http://msdn2.microsoft.com/en-us/library/aa479045.aspx, last updated for ASP.NET 1.1 in March, 2002. This article has not been updated for ASP.NET 2.0 and is not distributed with the Visual Studio 2005 documentation.

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

Subscribe to Posts [Atom]