CompareValidator.ValueToCompare 属性

定义

获取或设置一个常量值,用于与用户在正被验证的输入控件中输入的值进行比较。

public:
 property System::String ^ ValueToCompare { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.Bindable(true)]
public string ValueToCompare { get; set; }
[System.Web.UI.Themeable(false)]
public string ValueToCompare { get; set; }
[<System.ComponentModel.Bindable(true)>]
member this.ValueToCompare : string with get, set
[<System.Web.UI.Themeable(false)>]
member this.ValueToCompare : string with get, set
Public Property ValueToCompare As String

属性值

用于与用户在正被验证的输入控件中输入的值进行比较的常量值。 默认值是 Empty

属性

示例

下面的代码示例演示如何使用 ValueToCompare 属性将用户在控件中 TextBox 输入的值与常量值进行比较。

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ 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>CompareValidator ValueToCompare Example</title>
<script runat="server">
 
      void Button_Click(Object sender, EventArgs e) 
      {

         Random rand_number = new Random();

         Compare1.ValueToCompare = rand_number.Next(1, 10).ToString();
         Compare1.Validate();
 
         if (Page.IsValid) 
         {
            lblOutput.Text = "You guessed correctly!!";
         }
         else 
         {
            lblOutput.Text = "You guessed poorly";
         }

         lblOutput.Text += "<br /><br />" + "The number is: " + Compare1.ValueToCompare;

      }
 
   </script>
 
</head>
<body>
 
   <form id="form1" runat="server">

      <h3>CompareValidator ValueToCompare Example</h3>

      <h5>Pick a number between 1 and 10:</h5>
      <asp:TextBox id="TextBox1" 
           runat="server"/>

      <br /><br />

      <asp:Button id="Button1"
           Text="Submit"
           OnClick="Button_Click"
           runat="server"/>

      <br /><br />
       
      <asp:CompareValidator id="Compare1" 
           ControlToValidate="TextBox1"
           ValueToCompare="0"  
           Type="Integer"
           EnableClientScript="False" 
           runat="server"/>
 
      <br />
       
      <asp:Label id="lblOutput" 
           Font-Names="verdana" 
           Font-Size="10pt" 
           runat="server"/>
 
   </form>
 
</body>
</html>
<%@ 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>CompareValidator ValueToCompare Example</title>
<script runat="server">
 
      Sub Button_Click(sender As Object, e As EventArgs) 

         Dim rand_number As New Random()

         Compare1.ValueToCompare = rand_number.Next(1, 10).ToString()
         Compare1.Validate()
 
         If Page.IsValid Then 
         
            lblOutput.Text = "You guessed correctly!!"
         
         Else 
         
            lblOutput.Text = "You guessed poorly"
         
         End If

         lblOutput.Text &= "<br /><br />" & "The number is: " & Compare1.ValueToCompare

      End Sub
 
   </script>
 
</head>
<body>
 
   <form id="form1" runat="server">

      <h3>CompareValidator ValueToCompare Example</h3>

      <h5>Pick a number between 1 and 10:</h5>
      <asp:TextBox id="TextBox1" 
           runat="server"/>

      <br /><br />

      <asp:Button id="Button1"
           Text="Submit"
           OnClick="Button_Click"
           runat="server"/>

      <br /><br />
       
      <asp:CompareValidator id="Compare1" 
           ControlToValidate="TextBox1"
           ValueToCompare="0"  
           Type="Integer"
           EnableClientScript="False" 
           runat="server"/>
 
      <br />
       
      <asp:Label id="lblOutput" 
           Font-Names="verdana" 
           Font-Size="10pt" 
           runat="server"/>
 
   </form>
 
</body>
</html>

注解

ValueToCompare使用 属性指定一个常量值,以便与正在验证的输入控件中用户输入的值进行比较。 如果此属性指定的常量值无法转换为属性指定的 BaseCompareValidator.Type 数据类型,则会引发异常。

注意

不要同时设置 ControlToCompareValueToCompare 属性。 可以将一个输入控件的值与另一个输入控件的值进行比较,也可以与常量值进行比较。 如果同时设置了这两个属性,则 ControlToCompare 属性优先。

无法通过主题或样式表主题设置此属性。 有关详细信息,请参阅 ThemeableAttributeASP.NET 主题和皮肤

适用于

另请参阅