BaseValidator.ForeColor 属性

定义

获取或设置验证失败后显示的消息的颜色。

public:
 virtual property System::Drawing::Color ForeColor { System::Drawing::Color get(); void set(System::Drawing::Color value); };
public override System.Drawing.Color ForeColor { get; set; }
member this.ForeColor : System.Drawing.Color with get, set
Public Overrides Property ForeColor As Color

属性值

一个 Color,表示验证失败时显示的消息的颜色。 默认值为 Red

示例

下面的代码示例演示如何使用 ForeColor 属性以蓝色显示验证消息。

重要

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

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
 
  void Button_Click(Object sender, EventArgs e) 
  {
    if (Page.IsValid)
    {
      MessageLabel.Text = "Page submitted successfully.";
    }
    else
    {
      MessageLabel.Text = "There is an error on the page.";
    }
  }
 
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>Validator Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>Validator Example</h3>
     
      Enter a number from 1 to 10.
      <asp:textbox id="NumberTextBox" 
        runat="server"/>

      <asp:rangevalidator id="NumberCompareValidator" 
        controltovalidate="NumberTextBox"
        enableclientscript="False"  
        type="Integer"
        display="Dynamic" 
        errormessage="Please enter a value from 1 to 10."
        maximumvalue="10"
        minimumvalue="1"  
        text="*"
        forecolor="Blue" 
        runat="server"/>

      <asp:requiredfieldvalidator id="TextBoxRequiredValidator" 
        controltovalidate="NumberTextBox"
        enableclientscript="False"
        display="Dynamic" 
        errormessage="Please enter a value."
        text="*"
        forecolor="Blue" 
        runat="server"/>

      <br /><br />

      <asp:button id="SubmitButton"
        text="Submit"
        onclick="Button_Click"
        runat="server"/>
 
      <br /><br />
       
      <asp:label id="MessageLabel" 
        runat="server"/>

      <br /><br />

      <asp:validationsummary
        id="ErrorSummary"
        runat="server"/>
 
    </form>
  </body>
</html>
<%@ Page Language="VB" AutoEventWireup="False" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
 
  Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SubmitButton.Click
 
    If Page.IsValid Then
    
      MessageLabel.Text = "Page submitted successfully."
    
    Else
    
      MessageLabel.Text = "There is an error on the page."
    
    End If
    
  End Sub
 
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>Validator Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>Validator Example</h3>
     
      Enter a number from 1 to 10.
      <asp:textbox id="NumberTextBox" 
        runat="server"/>

      <asp:rangevalidator id="NumberCompareValidator" 
        controltovalidate="NumberTextBox"
        enableclientscript="False"  
        type="Integer"
        display="Dynamic" 
        errormessage="Please enter a value from 1 to 10."
        maximumvalue="10"
        minimumvalue="1"  
        text="*"
        forecolor="Blue" 
        runat="server"/>

      <asp:requiredfieldvalidator id="TextBoxRequiredValidator" 
        controltovalidate="NumberTextBox"
        enableclientscript="False"
        display="Dynamic" 
        errormessage="Please enter a value."
        text="*"
        forecolor="Blue" 
        runat="server"/>

      <br /><br />

      <asp:button id="SubmitButton"
        text="Submit"
        runat="server"/>
 
      <br /><br />
       
      <asp:label id="MessageLabel" 
        runat="server"/>

      <br /><br />

      <asp:validationsummary
        id="ErrorSummary"
        runat="server"/>
 
    </form>
  </body>
</html>

注解

ForeColor使用 属性为验证失败时在验证控件中显示的消息指定自定义文本颜色。

适用于

另请参阅