IValidator.ErrorMessage 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
當由類別實作時,取得或設定條件驗證失敗時所產生的錯誤訊息文字。
public:
property System::String ^ ErrorMessage { System::String ^ get(); void set(System::String ^ value); };
public string ErrorMessage { get; set; }
member this.ErrorMessage : string with get, set
Public Property ErrorMessage As String
屬性值
要產生的錯誤訊息。
範例
重要
這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。
<!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>IValidator Example demonstrating IsValid & ErrorMessage</title>
<script language="C#" runat="server">
void Button_Click(Object sender, EventArgs e)
{
// Generating the random number.
Random rand_s = new Random();
myCompareValidate.ValueToCompare = rand_s.Next(1, 10).ToString();
// Setting the ErrorMessage.
myCompareValidate.ErrorMessage="Try Again!!";
myCompareValidate.Validate();
// Check for Validity of control.
if ((myCompareValidate.IsValid) && (myTextBox.Text != ""))
{
labelOutput.Text = "You guessed correctly!!";
labelOutput.ForeColor = System.Drawing.Color.Blue;
}
else
{
labelOutput.Text = "You guessed poorly";
labelOutput.ForeColor = System.Drawing.Color.Black;
}
labelOutput.Text += "<br /><br />" + "The number is: " +
myCompareValidate.ValueToCompare;
}
</script>
</head>
<body>
<form runat="server" id="myForm">
<h3>IValidator Example demonstrating IsValid & ErrorMessage</h3>
<h5>Guess!! a number between 1 and 10 :</h5>
<asp:TextBox id="myTextBox" runat="server" />
<asp:CompareValidator id="myCompareValidate"
ControlToValidate="myTextBox" ValueToCompare="0"
EnableClientScript="False" Type="Integer" Text="*"
runat="server" />
<br />
<asp:Button Text="Submit" OnClick="Button_Click" runat="server" />
<br />
<asp:Label id="labelOutput" runat="server" />
<br />
<asp:ValidationSummary id="Summary1" runat="server" />
</form>
</body>
</html>
<!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>IValidator Example demonstrating IsValid & ErrorMessage</title>
<script language="VB" runat="server">
Sub Button_Click(sender As [Object], e As EventArgs)
' Generating a random number.
Dim rand_s As New Random()
myCompareValidate.ValueToCompare = rand_s.Next(1, 10).ToString()
' Set the ErrorMessage.
myCompareValidate.ErrorMessage = "Try Again!!"
myCompareValidate.Validate()
' Check for Validity of control.
If myCompareValidate.IsValid And myTextBox.Text <> "" Then
labelOutput.Text = "You guessed correctly!!"
labelOutput.ForeColor = System.Drawing.Color.Blue
Else
labelOutput.Text = "You guessed poorly"
labelOutput.ForeColor = System.Drawing.Color.Black
End If
labelOutput.Text += "<br /><br />" + "The number is: " + _
myCompareValidate.ValueToCompare
End Sub 'Button_Click
</script>
</head>
<body>
<form runat="server" id="myForm">
<h3>IValidator Example demonstrating IsValid & ErrorMessage</h3>
<h5>Guess!! a number between 1 and 10:</h5>
<asp:TextBox id="myTextBox" runat="server" />
<asp:CompareValidator id="myCompareValidate"
ControlToValidate="myTextBox" ValueToCompare="0"
EnableClientScript="False" Type="Integer" Text="*"
runat="server" />
<br />
<asp:Button Text="Submit" OnClick="Button_Click" runat="server" />
<br />
<asp:Label id="labelOutput" runat="server" />
<br />
<asp:ValidationSummary id="Summary1" runat="server" />
</form>
</body>
</html>
備註
ASP.NET 開發人員在驗證伺服器控制元件的開頭標記中以宣告方式存取此屬性。 如需詳細資訊,請參閱 驗證伺服器控件語法。