Export (0) Print
Expand All

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

Community Additions

ADD
Show:
© 2014 Microsoft