Sunday, September 03, 2006

 

Conveniences of controls

As one of its undisclosed "features" ASP.NET 2.0 will conveniently inhibit control events when you try to use "too many" of them. For example, suppose you need to confirm a user action and also validate that the user has specified an action to take. You might think you could use a standard approach -- a JavaScript function for OnClientClick with a Button and a RequiredFieldValidator in the < body > section of your .aspx file:

    < script type="text/javascript" >
    function ConfirmActivate()
    {
        var bConfirm = confirm('Confirm Activate');
        return bConfirm;
    }
    < /script >
    
    < asp:Button ID="ofButtonActivate" runat="server"
     Style="left: 8px; position: absolute; top: 24px;"
     Text="Activate" Width="176px" OnClick="Activate_Click"
     ValidationGroup="Test" Font-Size="Large"
     OnClientClick="return ConfirmActivate()" / >
    
    < Asp:TextBox ID="ofTextActivate" runat="server"
     Style="left: 200px; position: absolute; top: 24px"
     Height="24px" Width="600px" / >
    
    < asp:RequiredFieldValidator ID="ofValidActivate"
     runat="server" ControlToValidate="ofTextActivate"
     ErrorMessage="Selection required" ValidationGroup="Test" / >
    
    < asp:ValidationSummary ID="ofValidSummary" runat="server"
     ShowSummary="False" ShowMessageBox="True"
     ValidationGroup="Test" / >


Apparently Microsoft doesn't think your users should be bothered with "too many" messages, so ASP.NET 2.0 will conveniently inhibit the ValidationSummary, and no popup message will appear. One solution is to take out the validators and provide your own validation in JavaScript, for example:

    function ConfirmActivate()
    {
        var oTextActivate = document.
         getElementById('< % = ofTextActivate.ClientID % >');
        var oText = oTextActivate.value;
        if (oText == null || oText.length < 1)
        {
            alert('Activation name required');
            return false;
        }
        var bCancel = confirm('Please confirm Activate');
        return bCancel;
    }


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

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

Subscribe to Posts [Atom]