共用方式為


CompareValidator.EvaluateIsValid 方法

定義

衍生類別中發生覆寫時,這個方法會包含程式碼以判斷輸入控制項中的值是否有效。

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

傳回

如果輸入控制項中的值有效,則為 true;否則為 false

範例

下列程式代碼範例示範如何在自定義伺服器控制項中覆寫 EvaluateIsValid 方法,如此一律會傳回兩個指定控件的值相同,false否則一律會true傳回 。

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 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

適用於