ImageButton.CausesValidation 屬性

定義

取得或設定值,指出按一下 ImageButton 控制項時,是否執行驗證。

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

屬性值

如果按一下 ImageButton 控制項時會執行驗證,則為 true;否則為 false。 預設值是 true

實作

屬性

範例

下列範例示範如何使用 CausesValidation 屬性來防止頁面驗證發生。 請注意方法如何 Validate 獨立啟動每個驗證控制項。

注意

下列程式碼範例會使用單一檔案程式碼模型,如果直接複製到程式碼後置檔案,可能無法正常運作。 此程式碼範例必須複製到副檔名為 .aspx 的空白文字檔。 如需Web Form程式碼模型的詳細資訊,請參閱ASP.NET Web Forms頁碼模型


<%@ 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> ImageButton CausesValidation Example </title>
<script runat="server">

      void SubmitButton_Click(Object sender, ImageClickEventArgs e)
      {
         
         // Determine which button was clicked.
         switch(((ImageButton)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> ImageButton 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:ImageButton ID="CityQueryButton"
                    ImageUrl="SubmitImage.jpg"
                    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:ImageButton ID="StateQueryButton"
                    ImageUrl="SubmitImage.jpg"
                    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> ImageButton CausesValidation Example </title>
<script runat="server">

      Sub SubmitButton_Click(sender As Object, e As ImageClickEventArgs)
         
         ' Determine which button was clicked.
         Select Case (CType(sender, ImageButton)).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> ImageButton 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:ImageButton ID="CityQueryButton"
                    ImageUrl="SubmitImage.jpg"
                    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:ImageButton ID="StateQueryButton"
                    ImageUrl="SubmitImage.jpg"
                    CausesValidation="False"
                    OnClick="SubmitButton_Click"
                    runat="server"/>
            </td>
         </tr>

      </table>

      <br /><br />

      <asp:Label ID="Message"
           runat="Server"/>

   </form>

</body>
</html>

備註

根據預設,按一下控制項時 ImageButton ,會執行頁面驗證。 頁面驗證會判斷頁面上與驗證控制項相關聯的輸入控制項是否通過驗證控制項所指定的驗證規則。

您可以使用 屬性來指定或判斷是否在用戶端和伺服器上 ImageButtonCausesValidation 執行驗證。 若要防止執行驗證,請將 CausesValidation 屬性設定為 false

此屬性通常會針對 [重設] 或 [清除] 按鈕設定為 false ,以防止在按一下按鈕時執行驗證。

當 屬性的值 CausesValidation 設定 true 為 時,您也可以使用 ValidationGroup 屬性來指定控制項造成驗證的驗證組 ImageButton 名。

這個屬性無法由佈景主題或樣式表主題設定。 如需詳細資訊,請參閱 ThemeableAttributeASP.NET 主題和麵板

適用於

另請參閱