This documentation is archived and is not being maintained.

IValidator.ErrorMessage Property

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

[Visual Basic]
Property ErrorMessage As String
[C#]
string ErrorMessage {get; set;}
[C++]
__property String* get_ErrorMessage();
__property void set_ErrorMessage(String*);
[JScript]
function get ErrorMessage() : String;function set ErrorMessage(String);

Property Value

The error message to generate.

Remarks

Web Forms page developers access this property declaratively in the opening tag of a validation server control. For more information, see Validation Server Controls.

Example

[Visual Basic] 
<HTML>
   <HEAD>
      <script language="VB" runat="server">

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

            ' Set the ErrorMessage.
            myCompareValidate.ErrorMessage = "Try Again!!"
            myCompareValidate.Validate()

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

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

  </Script>
</HEAD>
    <body>
       <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" />
       </form>
    </body>
</HTML>

[C#] 
<HTML>
  <HEAD>
    <script language="C#" runat="server">

     void Button_Click(Object sender, EventArgs e)
     {
        // Generating the random number.
        Random rand_number = new Random();
        myCompareValidate.ValueToCompare = rand_number.Next(1, 10).ToString();

        // Setting the ErrorMessage.
        myCompareValidate.ErrorMessage="Try Again!!";
        myCompareValidate.Validate();

        // Check for Validity of control.
        if ((myCompareValidate.IsValid) && (myTextBox.Text != ""))
        {
           labelOutput.Text = "You guessed correctly!!";
           labelOutput.ForeColor = System.Drawing.Color.Blue;
        }
        else
        {
           labelOutput.Text =  "You guessed poorly";
           labelOutput.ForeColor = System.Drawing.Color.Black;
        }

        labelOutput.Text += "<br><br>" + "The number is: " +
           myCompareValidate.ValueToCompare;
     }

  </script>
</HEAD>
    <body>
      <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" />
     </form>
  </body>
</HTML>

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

IValidator Interface | IValidator Members | System.Web.UI Namespace | BaseValidator | Web Forms Validation

Show: