This documentation is archived and is not being maintained.

Displaying Custom Error Messages for ASP.NET Server Controls

You do not have to use the validation control to output a response when the 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

  • In the Page_Load event handler, call the Validate method, then check the page's IsValid property and conditionally add text or a control, or change the properties (for example, color) of a control. The following example displays the label "All entries are valid" when the IsValid property is true, and "There are one or more invalid entries" when the property is false.
    ' Visual Basic
    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
    
    // C#
    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

Introduction to Validating User Input in Web Forms | Controlling Validation Error Message Display for ASP.NET Server Controls | Formatting Validation Error Messages for ASP.NET Server Controls

Show: