Button.CausesValidation 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定值,指出按一下 Button 控制項時,是否執行驗證。
public:
property bool CausesValidation { bool get(); void set(bool value); };
public:
virtual property bool CausesValidation { bool get(); void set(bool value); };
[System.ComponentModel.Bindable(false)]
public bool CausesValidation { get; set; }
[System.Web.UI.Themeable(false)]
public virtual bool CausesValidation { get; set; }
[<System.ComponentModel.Bindable(false)>]
member this.CausesValidation : bool with get, set
[<System.Web.UI.Themeable(false)>]
member this.CausesValidation : bool with get, set
Public Property CausesValidation As Boolean
Public Overridable Property CausesValidation As Boolean
屬性值
如果按一下 Button 控制項時會執行驗證,則為 true
;否則為 false
。 預設值是 true
。
實作
- 屬性
範例
下列程式代碼範例示範如何使用 CausesValidation 屬性來防止頁面驗證發生。 請注意, Validate 方法會獨立啟動每個驗證控件。
重要
這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,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 runat="server">
<title> Button CausesValidation Example </title>
<script runat="server">
void SubmitButton_Click(Object sender, EventArgs e)
{
// Determine which button was clicked.
switch(((Button)sender).ID)
{
case "CityQueryButton":
// Validate only the controls used for the city query.
CityReqValidator.Validate();
// Take the appropriate action if the controls pass validation.
if (CityReqValidator.IsValid)
{
Message.Text = "You have chosen to run a query for the following city: " +
CityTextBox.Text;
}
break;
case "StateQueryButton":
// Validate only the controls used for the state query.
StateReqValidator.Validate();
// Take the appropriate action if the controls pass validation.
if (StateReqValidator.IsValid)
{
Message.Text = "You have chosen to run a query for the following state: " +
StateTextBox.Text;
}
break;
default:
// If the button clicked isn't recognized, erase the message on the page.
Message.Text = "";
break;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3> Button CausesValidation Example </h3>
<table border="1" cellpadding="10">
<tr>
<td>
<b>Enter city to query.</b> <br />
<asp:TextBox ID="CityTextBox"
runat="server"/>
<asp:RequiredFieldValidator ID="CityReqValidator"
ControlToValidate="CityTextBox"
ErrorMessage="<br />Please enter a city."
Display="Dynamic"
EnableClientScript="False"
runat="server"/>
</td>
<td valign="bottom">
<asp:Button ID="CityQueryButton"
Text="Submit"
CausesValidation="False"
OnClick="SubmitButton_Click"
runat="server"/>
</td>
</tr>
<tr>
<td>
<b>Enter state to query.</b> <br />
<asp:TextBox ID="StateTextBox"
runat="server"/>
<asp:RequiredFieldValidator ID="StateReqValidator"
ControlToValidate="StateTextBox"
ErrorMessage="<br />Please enter a state."
Display="Dynamic"
EnableClientScript="False"
runat="server"/>
</td>
<td valign="bottom">
<asp:Button ID="StateQueryButton"
Text="Submit"
CausesValidation="False"
OnClick="SubmitButton_Click"
runat="server"/>
</td>
</tr>
</table>
<br /><br />
<asp:Label ID="Message"
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 runat="server">
<title> Button CausesValidation Example </title>
<script runat="server">
Sub SubmitButton_Click(sender As Object, e As EventArgs)
' Determine which button was clicked.
Select Case (CType(sender, Button)).ID
Case "CityQueryButton"
' Validate only the controls used for the city query.
CityReqValidator.Validate()
' Take the appropriate action if the controls pass validation.
If CityReqValidator.IsValid Then
Message.Text = "You have chosen to run a query for the following city: " & _
CityTextBox.Text
End If
Case "StateQueryButton"
' Validate only the controls used for the state query.
StateReqValidator.Validate()
' Take the appropriate action if the controls pass validation.
If StateReqValidator.IsValid Then
Message.Text = "You have chosen to run a query for the following state: " & _
StateTextBox.Text
End If
Case Else
' If the button clicked isn't recognized, erase the message on the page.
Message.Text = ""
End Select
End Sub
</script>
</head>
<body>
<form id="form1" runat="server">
<h3> Button CausesValidation Example </h3>
<table border="1" cellpadding="10">
<tr>
<td>
<b>Enter city to query.</b> <br />
<asp:TextBox ID="CityTextBox"
runat="server"/>
<asp:RequiredFieldValidator ID="CityReqValidator"
ControlToValidate="CityTextBox"
ErrorMessage="<br />Please enter a city."
Display="Dynamic"
EnableClientScript="False"
runat="server"/>
</td>
<td valign="bottom">
<asp:Button ID="CityQueryButton"
Text="Submit"
CausesValidation="False"
OnClick="SubmitButton_Click"
runat="server"/>
</td>
</tr>
<tr>
<td>
<b>Enter state to query.</b> <br />
<asp:TextBox ID="StateTextBox"
runat="server"/>
<asp:RequiredFieldValidator ID="StateReqValidator"
ControlToValidate="StateTextBox"
ErrorMessage="<br />Please enter a state."
Display="Dynamic"
EnableClientScript="False"
runat="server"/>
</td>
<td valign="bottom">
<asp:Button ID="StateQueryButton"
Text="Submit"
CausesValidation="False"
OnClick="SubmitButton_Click"
runat="server"/>
</td>
</tr>
</table>
<br /><br />
<asp:Label ID="Message"
runat="Server"/>
</form>
</body>
</html>
備註
根據預設,按兩下控件時 Button ,會執行頁面驗證。 頁面驗證會判斷頁面上與驗證控件相關聯的輸入控件是否通過驗證控件所指定的驗證規則。
您可以使用 屬性來指定或判斷是否在用戶端和伺服器上ButtonCausesValidation執行驗證。 若要防止執行驗證,請將 CausesValidation 屬性設定為 false
。
注意
當您使用 屬性回傳至不同的頁面時,PostBackUrl應該將 CausesValidation 屬性false
設定為 。 在回傳至不同的頁面時,您應該明確檢查驗證。 如需範例,請參閱 屬性的 PostBackUrl 一節。
此屬性通常會針對 或 clear
按鈕設定為 false
reset
,以防止在按鍵時執行驗證。
當 屬性的值 CausesValidation 設定 true
為 時,您也可以使用 ValidationGroup 屬性來指定控件造成驗證的驗證組 Button 名。
這個屬性無法由佈景主題或樣式表主題設定。 如需詳細資訊,請參閱 ThemeableAttribute 和 ASP.NET 主題和外觀。