Compartir a través de


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
        ValidationControl1.Validate()
        If (ValidationControl1.IsValid) Then
            lblOutput.Text = "All entries are valid."
        Else
            lblOutput.Text = "There are one or more invalid entries."
        End If
    End If
    
    if (this.IsPostBack)
      {
          ValidationControl1.Validate();
          if (ValidationControl1.IsValid)
          {
              lblOutput.Text = "All entries are valid.";
          }
          else
          {
              lblOutput.Text = "There are one or more invalid entries.";
          }
      }
    

See Also

Tasks

How to: Control Validation Error Message Display for ASP.NET Server Controls

How to: Format Validation Error Messages for ASP.NET Server Controls

Concepts

Types of Validation for ASP.NET Server Controls

Client-Side Validation for ASP.NET Server Controls

Other Resources

Validation ASP.NET Controls