📜  ASP.Net Web窗体CompareValidator

📅  最后修改于: 2020-12-28 00:37:18             🧑  作者: Mango

ASP.NET验证

在本章中,我们将讨论Web表单中的数据验证。为了执行验证,ASP.NET提供了可自动检查用户输入并且不需要代码的控件。我们还可以为我们的应用程序创建自定义验证。

ASP.NET验证控件

以下是验证控件

Validator Description
CompareValidator It is used to compare the value of an input control against a value of another input control.
RangeValidator It evaluates the value of an input control to check the specified range.
RegularExpressionValidator It evaluates the value of an input control to determine whether it matches a pattern defined by a regular expression.
RequiredFieldValidator It is used to make a control required.
ValidationSummary It displays a list of all validation errors on the Web page.

ASP.NET CompareValidator控件

该验证器根据指定的运算符对于另一个输入控件评估一个输入控件的值。

我们可以用比较运算符,如:小于,等于,大于等等。

注意:如果输入字段为空,将不执行验证。

CompareValidator属性

Property Description
AccessKey It is used to set keyboard shortcut for the control.
TabIndex The tab order of the control.
BackColor It is used to set background color of the control.
BorderColor It is used to set border color of the control.
BorderWidth It is used to set width of border of the control.
Font It is used to set font for the control text.
ForeColor It is used to set color of the control text.
Text It is used to set text to be shown for the control.
ToolTip It displays the text when mouse is over the control.
Visible To set visibility of control on the form.
Height It is used to set height of the control.
Width It is used to set width of the control.
ControlToCompare It takes ID of control to compare with.
ControlToValidate It takes ID of control to validate.
ErrorMessage It is used to display error message when validation failed.
Operator It is used set comparison operator.

在这里,在下面的示例中,我们将使用CompareValidator控制器来验证用户输入。示例的源代码如下。

// compare_validator_demo.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="compare_validator_demo.aspx.cs" 
Inherits="asp.netexample.compare_validator_demo" %>







First value
Second value It should be greater than first value
< asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="secondval" ControlToValidate="firstval" Display="Dynamic" ErrorMessage="Enter valid value" ForeColor="Red" Operator="LessThan" Type="Integer">

输出: