CompareValidator.EvaluateIsValid 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当在派生类中重写时,此方法包含代码,以确定输入控件中的值是否有效。
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 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述。
<%@ 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