共用方式為


CompareValidator.ControlPropertiesValid 方法

定義

檢查控制項的屬性值是否有效。

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

傳回

如果控制項屬性有效,則為 true,否則為 false

例外狀況

ControlToValidateControlToCompare 有相同的 ID

-或-

目標屬性的值無法轉換成預期的 Type

範例

下列程式代碼範例示範如何在自定義伺服器控制項中覆寫 ControlPropertiesValid 方法,讓它一律傳回可見屬性的值,只要 ControlToCompare 控件的 CompareValidator 屬性位於頁面上且包含驗證屬性。

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" 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 - ControlPropertiesValid - 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 - ControlPropertiesValid - C# Example</h3>

      <asp:TextBox id="TextBox1" runat="server">123</asp:TextBox><br />

      <aspSample:CustomCompareValidatorControlPropertiesValid
        id="CompareValidator1"
        runat="server"
        Display="Dynamic"
        ErrorMessage="The value of TextBox1 must be '456'."
        ControlToValidate="TextBox1"
        ValueToCompare="456" /><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 - ControlPropertiesValid - VB.NET Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">
      <h3>Custom CompareValidator - ControlPropertiesValid - C# Example</h3>
            <asp:TextBox id="TextBox1" runat="server">123</asp:TextBox><br />
            <aspSample:CustomCompareValidatorControlPropertiesValid
             id="CompareValidator1" runat="server" Display="Dynamic"
             ErrorMessage="The value of TextBox1 must be '456'."
             ControlToValidate="TextBox1" ValueToCompare="456" /><br />
             <asp:Button id="Button1" runat="server" Text="Button" />
    </form>
  </body>
</html>
using System.Web;
using System.Security.Permissions;

namespace Samples.AspNet.CS.Controls
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    public sealed class CustomCompareValidatorControlPropertiesValid : System.Web.UI.WebControls.CompareValidator
    {
        protected override bool ControlPropertiesValid()
        {
            // Determine whether the ControlToValidate is on the page and contains a validation properties. 
            base.CheckControlValidationProperty(this.ControlToValidate, "ControlToValidate");

            // If the control is visible, then control is valid and is ready for validation.
            System.Web.UI.Control control = this.FindControl(this.ControlToValidate);
            return control.Visible;
        }
    }
}
Imports System.Web
Imports System.Security.Permissions

Namespace Samples.AspNet.VB.Controls
    <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public NotInheritable Class CustomCompareValidatorControlPropertiesValid
        Inherits System.Web.UI.WebControls.CompareValidator

        Protected Overrides Function ControlPropertiesValid() As Boolean

            ' Determine whether the ControlToValidate is on the page and contains a validation properties. 
            MyBase.CheckControlValidationProperty(Me.ControlToValidate, "ControlToValidate")

            ' If the control is visible, then control is valid and ready for validation.
            Dim control As System.Web.UI.Control = Me.FindControl(Me.ControlToValidate)
            If control.Visible = True Then
                Return True
            Else
                Return False
            End If
        End Function
    End Class
End Namespace

適用於