RangeValidator.EvaluateIsValid Method

Definition

Determines whether the content in the input control is valid.

C#
protected override bool EvaluateIsValid();

Returns

true if the 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 validates that the value exists and is within the maximum and minimum values specified for the RangeValidator.

ASP.NET (C#)
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS" %>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!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 RangeValidator - EvaluateIsValid - C# Example</title>
    <script runat="server">
      void ButtonClick(Object sender, EventArgs e)
      {
        if (Page.IsValid)
        {
          Label1.Text="Page is valid.";
        }
        else
        {
          Label1.Text="Page is not valid!!";
        }
      }
    </script>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">
      <h3>Custom RangeValidator - EvaluateIsValid - C# Example</h3>

      Enter a number from 1 to 10:<br />
      <asp:TextBox id="TextBox1" Runat="server" />

      <aspSample:CustomRangeValidatorEvaluateIsValid 
        id="RangeValidator1" 
        runat="server" 
        ControlToValidate="TextBox1" 
        MinimumValue="1" 
        MaximumValue="10" 
        Type="Integer" 
        EnableClientScript="false" 
        Text="The value must be from 1 to 10!" />
        
      <br />
      <br />
      <asp:Button id="Button1" Text="Submit" OnClick="ButtonClick" runat="server" /><br />
      <br />

      <asp:Label id="Label1" runat="server" />
    </form>
  </body>
</html>
C#
using System.Web;
using System.Security.Permissions;

namespace Samples.AspNet.CS.Controls
{
  [AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)]
  public sealed class CustomRangeValidatorEvaluateIsValid : System.Web.UI.WebControls.RangeValidator
  {
    protected override bool EvaluateIsValid()
    {
      // Get the value of the control to validate.
      string controlValue = GetControlValidationValue(ControlToValidate);

      // If no value was entered, show the validation error by returning false.
      if (controlValue.Trim().Length == 0) 
      {
        return false;
      }

      // Compare the ControlToValidate's value against the minimum and maximum values.
      return(Compare(controlValue, this.MinimumValue, System.Web.UI.WebControls.ValidationCompareOperator.GreaterThanEqual, this.Type) &&
        Compare(controlValue, this.MaximumValue, System.Web.UI.WebControls.ValidationCompareOperator.LessThanEqual, this.Type));
    }            
  }
}

Applies to

Product Versions
.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