IValidator.IsValid 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
當由類別實作時,取得或設定數值,表示在指定的控制項中的使用者輸入內容是否通過驗證。
public:
property bool IsValid { bool get(); void set(bool value); };
public bool IsValid { get; set; }
member this.IsValid : bool with get, set
Public Property IsValid As Boolean
屬性值
如果內容無效,則為 true
,否則為 false
。
範例
重要
這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,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>