Home / AJAX

HOW TO Use the ValidatorCallout Control

RSS
Modified on 2009/12/24 04:15 by Chris Muench Categorized as Uncategorized

ValidatorCallout Demonstration



ValidatorCallout Description

ValidatorCallout is an ASP.NET AJAX extender that enhances the functionality of existing ASP.NET validators. To use this control, add an input field and a validator control as you normally would. Then add the ValidatorCallout and set its TargetControlID property to reference the validator control.

Video - How Do I: Use the ASP.NET AJAX ValidatorCallout Control?

ValidatorCallout Client Code Sample

NO CLIENT SIDE SAMPLE




ValidatorCallout Client Properties

ValidatorCallout Server Code Sample

        <table>
            <tr>
                <td>Name:</td>
                <td><asp:TextBox runat="server" ID="NameTextBox"
                    BorderStyle="solid" BorderWidth="1px" BorderColor="#a9a9a9" /></td>
            </tr>
            <tr>
                <td>Phone Number:</td>
                <td><asp:TextBox runat="server" ID="PhoneNumberTextBox"
                    BorderStyle="solid" BorderWidth="1px" BorderColor="#a9a9a9" /></td>
            </tr>
        </table>
        <br />
        
        <asp:RequiredFieldValidator runat="server" ID="NReq"
            ControlToValidate="NameTextBox"
            Display="None"
            ErrorMessage="<b>Required Field Missing</b><br />A name is required." />
        <ajaxToolkit:ValidatorCalloutExtender runat="Server" ID="NReqE"
            TargetControlID="NReq"
            HighlightCssClass="validatorCalloutHighlight" />
            
        <asp:RequiredFieldValidator runat="server" ID="PNReq"
            ControlToValidate="PhoneNumberTextBox"
            Display="None"
            ErrorMessage="<b>Required Field Missing</b><br />A phone number is required.<div style='margin-top:5px;padding:5px;border:1px solid #e9e9e9;background-color:white;'><b>Other Options:</b><br /><a href='javascript:alert(&quot;No phone number available in profile.&quot;);'>Extract from Profile</a></div>" />
        <ajaxToolkit:ValidatorCalloutExtender runat="Server" ID="PNReqE"
            TargetControlID="PNReq"
            HighlightCssClass="validatorCalloutHighlight"
            Width="350px" />
            
        <asp:RegularExpressionValidator runat="server" ID="PNRegEx"
            ControlToValidate="PhoneNumberTextBox"
            Display="None"
            ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}"
            ErrorMessage="<b>Invalid Field</b><br />Please enter a phone number in the format:<br />(###) ###-####" />
        <ajaxToolkit:ValidatorCalloutExtender runat="Server" ID="PNReqEx"
            TargetControlID="PNRegEx"
            HighlightCssClass="validatorCalloutHighlight" />
             
        <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_OnClick" /><br /><br />
        <asp:UpdatePanel runat="server" ID="up1">
            <ContentTemplate>
                <asp:Label id="lblMessage" runat="server" />
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
            </Triggers>
        </asp:UpdatePanel>


ValidatorCallout Server Properties

The properties in italics are optional.

<ajaxToolkit:ValidatorCalloutExtender 
    runat="Server"
    ID="PNReqE"
    TargetControlID="PNReq" 
    Width="350px"
    HighlightCssClass="highlight" 
    WarningIconImageUrl="warning.gif"
    CloseImageUrl="close.gif" />
  • TargetControlID - The ID of the Validator to extend
  • Width - The width of the callout
  • CssClass - Name of the CSS class used to style the ValidatorCallout. See the ValidatorCallout Theming section for more information.
  • HighlightCssClass - A CssClass to apply to the invalid field
  • WarningIconImageUrl - The path to a custom warning icon image
  • CloseImageUrl - The path to a custom close image
  • Animations - Generic animations for the ValidatorCallout extender. See the Using Animations walkthrough and Animation Reference for more details.
    • OnShow - The OnShow animation will be played each time the validation popup is displayed. The popup will be positioned correctly but hidden. The animation can use to display the popup along with any other visual effects.
    • OnHide - The OnHide animation will be played each time the validation popup is hidden.

ValidatorCallout Known Issues

The callouts do not currently display automatically after a server post-back and will only work for custom validators which utilize client-side validation. Even after a post-back the callout will display when the form is re-validated when a postback is attempted again.

ValidatorCallout Theming

You can change the look and feel of the ValidatorCallout using the ValidatorCallout CssClass property. The ValidatorCallout has a predefined set of CSS classes that can be overridden. It has a default style which is embedded as a WebResource and is a part of the Toolkit assembly that has styles set for all the sub-classes. You can find the default styles in the Toolkit solution in the "AjaxControlToolkit\ValidatorCallout\ValidatorCallout.css" file. If your CssClass does not provide values for any of those then it falls back to the default value. In the example above the default style is used. To customize the same the user would have to set the CssClass property to the name of the CSS style and define the styles for the individual classes so that the various elements in a ValidatorCallout control can be styled accordingly. For example, if the CssClass property was set to "CustomValidatorCalloutStyle", this is how the css to style the border and background color would look:

.CustomValidatorCalloutStyle div, 
.CustomValidatorCalloutStyle td { 
    border:solid 1px blue; background-color: #ADD8E6; 
}

ValidatorCallout Css classes

.ajax__validatorcallout_popup_table: The popup table.
.ajax__validatorcallout_popup_table_row: The popup table row.
.ajax__validatorcallout_callout_cell: The callout cell.
.ajax__validatorcallout_callout_table: The table in the callout cell.
.ajax__validatorcallout_callout_table_row: The callout table row.
.ajax__validatorcallout_error_message_cell: The error message cell. 
.ajax__validatorcallout_icon_cell: The warning icon cell.
.ajax__validatorcallout_close_button_cell: The close button cell. 
.ajax__validatorcallout_arrow_cell: The arror cell.
.ajax__validatorcallout_innerdiv: Inner div of a cell. Used in the close button cell and the arrow cell.

ValidatorCallout Additional Code Samples