CompareValidator.EvaluateIsValid Method

Definition

When overridden in a derived class, this method contains the code to determine whether the value in the input control is valid.

C#
protected override bool EvaluateIsValid();

Returns

true if the value in the input control is valid; otherwise, false.

Examples

The following code example demonstrates how to override the EvaluateIsValid method in a custom server control so that it always returns true if the values of the two specified controls are the same and false otherwise.

Dôležité

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.

ASP.NET (C#)
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet" Assembly="Samples.AspNet.CS" %>
<%@ Page language="c#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Custom CompareValidator - EvaluateIsValid - C# Example</title>
    <script runat="server">
      void Page_Load(Object sender, EventArgs e) 
      {
        // Run the Page Validate method in order to force 
        // the CompareValidate to show it's error message.
        Page.Validate();
      }
    </script>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <h3>Custom CompareValidator - EvaluateIsValid - C# Example</h3>
            
      <asp:TextBox id="TextBox1" runat="server">123</asp:TextBox><br />
      
      <asp:TextBox id="TextBox2" runat="server">123</asp:TextBox>
      
      <aspSample:CustomCompareValidatorEvaluateIsValid 
        id="CompareValidator1" 
        runat="server" 
        ErrorMessage="Value in TextBox2 does not match value in TextBox1." 
        Display="Dynamic" 
        ControlToCompare="TextBox2" 
        ControlToValidate="TextBox1" /><br />
      
      <asp:Button id="Button1" runat="server" Text="Button" />
       
       </form>
    </body>
</html>
C#
using System.Web;
using System.Security.Permissions;

namespace Samples.AspNet
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    public sealed class CustomCompareValidatorEvaluateIsValid : System.Web.UI.WebControls.CompareValidator
    {
    protected override bool EvaluateIsValid()
    {
      // Get the values from the two controls
      string controlToValidateValue = this.GetControlValidationValue(this.ControlToValidate);
      string controlToCompareValue = this.GetControlValidationValue(this.ControlToCompare);

      // If the values are the same, return true, else return false.
      if (System.String.Compare(controlToValidateValue, 0, controlToCompareValue, 0, controlToCompareValue.Length, false, System.Globalization.CultureInfo.InvariantCulture) == 0) 
      {
        return true;
      }
      else
      {
        return false;
      }
    }
  }
}

Applies to

Produkt Verzie
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1