Button的CausesValidation屬性的作用:
獲取或設(shè)置一個(gè)值,該值指示在單擊 Button 控件時(shí)是否執(zhí)行驗(yàn)證。
ASP.NET
<asp:Button CausesValidation="True|False" />
屬性值
類型:System..::.Boolean
如果在單擊 Button 控件時(shí)執(zhí)行驗(yàn)證,則為 true;否則為 false。默認(rèn)值為 true。
實(shí)現(xiàn)
IButtonControl..::.CausesValidation
備注
默認(rèn)情況下,單擊 Button 控件時(shí)執(zhí)行頁驗(yàn)證。頁驗(yàn)證確定頁上與驗(yàn)證控件關(guān)聯(lián)的輸入控件是否均通過該驗(yàn)證控件所指定的驗(yàn)證規(guī)則。
通過使用 CausesValidation 屬性,可以指定或確定當(dāng)單擊 Button 控件時(shí),是否同時(shí)在客戶端和服務(wù)器上執(zhí)行驗(yàn)證。若要禁止執(zhí)行驗(yàn)證,請(qǐng)將 CausesValidation 屬性設(shè)置為 false。
說明:
當(dāng)使用 PostBackUrl 屬性回發(fā)至不同頁面時(shí),應(yīng)將 CausesValidation 屬性設(shè)置為 false。在回發(fā)至不同頁面時(shí),應(yīng)對(duì)驗(yàn)證進(jìn)行顯式檢查。有關(guān)示例,請(qǐng)參見 PostBackUrl 屬性的“備注”部分。
對(duì)于 reset 或 clear 按鈕,此屬性通常設(shè)置為 false,以防止在單擊其中某個(gè)按鈕時(shí)執(zhí)行驗(yàn)證。
當(dāng) CausesValidation 屬性的值設(shè)置為 true 時(shí),還可以使用 ValidationGroup 屬性來指定 Button 控件引發(fā)驗(yàn)證時(shí)所對(duì)應(yīng)的驗(yàn)證組的名稱。
無法通過主題或樣式表主題設(shè)置此屬性。有關(guān)更多信息,請(qǐng)參見 ThemeableAttribute和 ASP.NET 主題和外觀概述。
示例
下面的代碼示例演示如何使用 CausesValidation 屬性防止發(fā)生頁驗(yàn)證。注意,Validate 方法會(huì)單獨(dú)激活各個(gè)驗(yàn)證控件。
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"
<html > <head id="Head1" 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">
<!-- 這里把 CausesValidation="false" 默認(rèn)時(shí)為true 會(huì)對(duì)整個(gè)頁面驗(yàn)證 (就是驗(yàn)證標(biāo)簽所做的驗(yàn)證) 這里如果沒有OnClick="SubmitButton_Click" 則不會(huì)執(zhí)行驗(yàn)證-->
<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>