Monday, June 04, 2007
Button validation and more
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.
<~asp:Button ID="ofButtonTest" runat="server"
OnClick="ButtonTest_Click" ValidationGroup="Submit" ... /~>
if (Page_Validators == null || ValidatorValidate == null
|| ValidatorUpdateIsValid == null || Page_BlockSubmit == null
|| ValidationSummaryOnSubmit == null || Page_IsValid == null
|| __defaultFired == null)
var oValidatorCount = Page_Validators.length;
for (var oValidatorIndex = 0;
oValidatorIndex < oValidatorCount; ++oValidatorIndex)
Page_BlockSubmit = !Page_IsValid;
__defaultFired = false;
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.
Subscribe to Posts [Atom]