How to: Display Server Side Custom Validation Messages for ASP.NET Server Controls

You do not have to use the validation control to output a response when the validator or page IsValid property is false. On both the client and server side you can create a custom response, such as a color change in a control or a font change for text on a label.

To display custom validation messages

  1. In the Page_Load event handler, call the Validate method of the validation control or of the page.

  2. Check the IsValid property of the validation control or page and conditionally add text or a control, or change the properties (for example, color) of a control.

    The following code example displays the text "All entries are valid" when the IsValid property is true, and the text "There are one or more invalid entries" when the property is false.

    If (Me.IsPostBack) Then
        If (ValidationControl1.IsValid) Then
            lblOutput.Text = "All entries are valid."
            lblOutput.Text = "There are one or more invalid entries."
        End If
    End If

See Also