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 Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

<%@ 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 主題和外觀

適用於

另請參閱