IValidator.ErrorMessage Property


When implemented by a class, gets or sets the error message text generated when the condition being validated fails.

Namespace:   System.Web.UI
Assembly:  System.Web (in System.Web.dll)

Property ErrorMessage As String

Property Value

Type: System.String

The error message to generate.

ASP.NET developers access this property declaratively in the opening tag of a validation server control. For more information, see Validation Server Control Syntax.

System_CAPS_security Security Note

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" >
    <title>IValidator Example demonstrating IsValid & ErrorMessage</title>
<script language="VB" runat="server">

         Sub Button_Click(sender As [Object], e As EventArgs)
            ' Generating a random number.
            Dim rand_s As New Random()
            myCompareValidate.ValueToCompare = rand_s.Next(1, 10).ToString()

            ' Set the ErrorMessage.
            myCompareValidate.ErrorMessage = "Try Again!!"

            ' Check for Validity of control.
            If myCompareValidate.IsValid And myTextBox.Text <> "" Then
               labelOutput.Text = "You guessed correctly!!"
               labelOutput.ForeColor = System.Drawing.Color.Blue
               labelOutput.Text = "You guessed poorly"
               labelOutput.ForeColor = System.Drawing.Color.Black
            End If

            labelOutput.Text += "<br /><br />" + "The number is: " + _
         End Sub 'Button_Click

       <form runat="server" id="myForm">
          <h3>IValidator Example demonstrating IsValid & ErrorMessage</h3>
          <h5>Guess!! a number between 1 and 10:</h5>
          <asp:TextBox id="myTextBox" runat="server" />
          <asp:CompareValidator id="myCompareValidate"
               ControlToValidate="myTextBox" ValueToCompare="0"
               EnableClientScript="False" Type="Integer" Text="*"
               runat="server" />
          <br />
          <asp:Button Text="Submit" OnClick="Button_Click" runat="server" />
          <br />
          <asp:Label id="labelOutput" runat="server" />
          <br />
          <asp:ValidationSummary id="Summary1" runat="server" />

.NET Framework
Available since 1.1
Return to top