次の方法で共有


RangeValidator.EvaluateIsValid メソッド

定義

入力コントロールの内容が有効であるかどうかを確認します。

protected:
 override bool EvaluateIsValid();
protected override bool EvaluateIsValid ();
override this.EvaluateIsValid : unit -> bool
Protected Overrides Function EvaluateIsValid () As Boolean

戻り値

コントロールが有効な場合は true。それ以外の場合は false

次のコード例では、カスタム サーバー コントロールの メソッドをオーバーライド EvaluateIsValid して、値が存在し、 に指定された最大値と最小値内にあることを常に検証する方法を RangeValidator示します。

<%@ 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>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB.Controls" Assembly="Samples.AspNet.VB" %>
<%@ Page Language="VB" 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 - VB.NET Example</title>
        <script runat="server">
            Sub ButtonClick(sender As Object, e As EventArgs)
                If Page.IsValid Then
                    Label1.Text="Page is valid."
                Else
                    Label1.Text="Page is not valid!"
                End If
            End Sub
        </script>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <h3>Custom RangeValidator - EvaluateIsValid - VB.NET 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>
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));
    }            
  }
}
Imports System.Web
Imports System.Security.Permissions

Namespace Samples.AspNet.VB.Controls
    <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public NotInheritable Class CustomRangeValidatorEvaluateIsValid
        Inherits System.Web.UI.WebControls.RangeValidator

        Protected Overrides Function EvaluateIsValid() As Boolean

            ' Get the value of the control to validate.
            Dim controlValue As String = GetControlValidationValue(ControlToValidate)

            ' If no value was entered, show the validation error by returning false.
            If controlValue.Trim().Length = 0 Then
                Return False
            End If

            ' Compare the ControlToValidate's value against the minimum and maximum values.
            Return Compare(controlValue, Me.MinimumValue, System.Web.UI.WebControls.ValidationCompareOperator.GreaterThanEqual, Me.Type) AndAlso _
                   Compare(controlValue, Me.MaximumValue, System.Web.UI.WebControls.ValidationCompareOperator.LessThanEqual, Me.Type)
        End Function
    End Class
End Namespace

適用対象