Button的CausesValidation屬性的作用:
獲取或設置一個值,該值指示在單擊 Button 控件時是否執(zhí)行驗證。
ASP.NET
<asp:Button CausesValidation="True|False" /> 屬性值 類型:System..::.Boolean 如果在單擊 Button 控件時執(zhí)行驗證,則為 true;否則為 false。默認值為 true。
實現(xiàn)
IButtonControl..::.CausesValidation 備注 默認情況下,單擊 Button 控件時執(zhí)行頁驗證。頁驗證確定頁上與驗證控件關聯(lián)的輸入控件是否均通過該驗證控件所指定的驗證規(guī)則。 通過使用 CausesValidation 屬性,可以指定或確定當單擊 Button 控件時,是否同時在客戶端和服務器上執(zhí)行驗證。若要禁止執(zhí)行驗證,請將 CausesValidation 屬性設置為 false。
說明:
當使用 PostBackUrl 屬性回發(fā)至不同頁面時,應將 CausesValidation 屬性設置為 false。在回發(fā)至不同頁面時,應對驗證進行顯式檢查。有關示例,請參見 PostBackUrl 屬性的“備注”部分。 對于 reset 或 clear 按鈕,此屬性通常設置為 false,以防止在單擊其中某個按鈕時執(zhí)行驗證。
當 CausesValidation 屬性的值設置為 true 時,還可以使用 ValidationGroup 屬性來指定 Button 控件引發(fā)驗證時所對應的驗證組的名稱。
無法通過主題或樣式表主題設置此屬性。有關更多信息,請參見 ThemeableAttribute和 ASP.NET 主題和外觀概述。
示例
下面的代碼示例演示如何使用 CausesValidation 屬性防止發(fā)生頁驗證。注意,Validate 方法會單獨激活各個驗證控件。 <%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <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" 默認時為true 會對整個頁面驗證 (就是驗證標簽所做的驗證) 這里如果沒有OnClick="SubmitButton_Click" 則不會執(zhí)行驗證--> <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> 本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/huazi123654/archive/2008/12/24/3597489.aspx
|
|
來自: 寒木蕭條 > 《aspDotNet》