다음을 통해 공유


CompareValidator.EvaluateIsValid 메서드

정의

파생된 클래스에서 재정의되면 이 메서드는 입력 컨트롤의 값이 유효한지 여부를 확인하는 코드를 포함합니다.

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

반환

Boolean

입력 컨트롤의 값이 유효하면 true이고, 유효하지 않으면 false입니다.

예제

다음 코드 예제에는 재정의 하는 방법을 보여 줍니다.는 EvaluateIsValid 항상 반환 되도록 컨트롤을 사용자 지정 서버에서 메서드 true 두 지정 된 컨트롤의 값이 동일한 경우 및 false 그렇지 않은 경우.

중요

이 예제에는 사용자 입력을 허용하는 텍스트 상자가 있으므로 보안상 위험할 수 있습니다. 기본적으로 ASP.NET 웹 페이지는 사용자 입력 내용에 스크립트 또는 HTML 요소가 포함되어 있지 않은지 확인합니다. 자세한 내용은 Script Exploits Overview를 참조하세요.

<%@ 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>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB.Controls" Assembly="Samples.AspNet.VB" %>
<%@ Page language="VB" %>
<!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 - VB.NET Example</title>
  </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>
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;
      }
    }
  }
}
Imports System.Web
Imports System.Security.Permissions
Namespace Samples.AspNet.VB.Controls
    <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public NotInheritable Class CustomCompareValidatorEvaluateIsValid
        Inherits System.Web.UI.WebControls.CompareValidator

        Protected Overrides Function EvaluateIsValid() As Boolean

            ' Get the values from the two controls
            Dim controlToValidateValue As String = Me.GetControlValidationValue(Me.ControlToValidate)
            Dim controlToCompareValue As String = Me.GetControlValidationValue(Me.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) Then
                Return True
            Else
                Return False
            End If
        End Function
    End Class
End Namespace

적용 대상