共用方式為


HtmlInputButton.CausesValidation 屬性

定義

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

public:
 property bool CausesValidation { bool get(); void set(bool value); };
public:
 virtual property bool CausesValidation { bool get(); void set(bool value); };
public bool CausesValidation { get; set; }
public virtual bool CausesValidation { get; set; }
member this.CausesValidation : bool with get, set
Public Property CausesValidation As Boolean
Public Overridable Property CausesValidation As Boolean

屬性值

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

範例

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

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  void SubmitButton_Click(Object sender, EventArgs e)
  {

    
    // Determine which button was clicked.
    switch (((HtmlInputButton)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.InnerHtml = "You have chosen to run a query for the following city: " +
             CityTextBox.Value;
        }

        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.InnerHtml = "You have chosen to run a query for the following state: " +
             StateTextBox.Value;
        }

        break;

      default:

        // If the button clicked is not recognized, erase the message on the page.
        Message.InnerHtml = "";

        break;

    }

  }

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>HtmlInputButton CausesValidation Example</title>
</head>
<body>
   <form id="form1" runat="server">
      <h3> HtmlInputButton CausesValidation Example </h3>

      <table border="1" cellpadding="10">
         <tr>
            <td>
               <b>Enter city to query.</b> <br />
               <input id="CityTextBox" 
                      type="Text"
                      runat="server"/>
               <asp:RequiredFieldValidator ID="CityReqValidator"
                    ControlToValidate="CityTextBox"
                    ErrorMessage="<br />Please enter a city."
                    Display="Dynamic"
                    EnableClientScript="False"
                    runat="server"/>
            </td>
            <td valign="bottom">
               <input id="CityQueryButton"
                      type="Submit"
                      value="Submit"
                      causesvalidation="False"
                      onserverclick="SubmitButton_Click"
                      runat="server"/>
            </td>
         </tr>

         <tr>
            <td>
               <b>Enter state to query.</b> <br />
               <input id="StateTextBox" 
                      type="Text" 
                      runat="server"/>
               <asp:RequiredFieldValidator ID="StateReqValidator"
                    ControlToValidate="StateTextBox"
                    ErrorMessage="<br />Please enter a state."
                    Display="Dynamic"
                    EnableClientScript="False"
                    runat="server"/>
            </td>
            <td valign="bottom">
               <input id="StateQueryButton"
                      type="Submit"
                      value="Submit"
                      causesvalidation="False"
                      onserverclick="SubmitButton_Click"
                      runat="server"/>
            </td>
         </tr>

      </table>

      <br /><br />

      <span id="Message"
            runat="Server"/>


   </form>
</body>
</html>
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  Sub SubmitButton_Click(ByVal sender As Object, ByVal e As EventArgs)
         
    ' Determine which button was clicked.
    Select Case (CType(sender, HtmlInputButton)).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.InnerHtml = "You have chosen to run a query for the following city: " & _
             CityTextBox.Value
               
        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.InnerHtml = "You have chosen to run a query for the following state: " & _
             StateTextBox.Value
               
        End If

      Case Else

        ' If the button clicked is not recognized, erase the message on the page.
        Message.InnerHtml = ""

    End Select
        
  End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head  runat="server">
    <title>HtmlInputButton CausesValidation Example</title>
</head>
<body>
   <form id="form1" runat="server">
      <h3> HtmlInputButton CausesValidation Example </h3>

      <table border="1" cellpadding="10">
         <tr>
            <td>
               <b>Enter city to query.</b> <br />
               <input id="CityTextBox" 
                      type="Text"
                      runat="server"/>
               <asp:RequiredFieldValidator ID="CityReqValidator"
                    ControlToValidate="CityTextBox"
                    ErrorMessage="<br />Please enter a city."
                    Display="Dynamic"
                    EnableClientScript="False"
                    runat="server"/>
            </td>
            <td valign="bottom">
               <input id="CityQueryButton"
                      type="Submit"
                      value="Submit"
                      causesvalidation="False"
                      onserverclick="SubmitButton_Click"
                      runat="server"/>
            </td>
         </tr>

         <tr>
            <td>
               <b>Enter state to query.</b> <br />
               <input id="StateTextBox" 
                      type="Text" 
                      runat="server"/>
               <asp:RequiredFieldValidator ID="StateReqValidator"
                    ControlToValidate="StateTextBox"
                    ErrorMessage="<br />Please enter a state."
                    Display="Dynamic"
                    EnableClientScript="False"
                    runat="server"/>
            </td>
            <td valign="bottom">
               <input id="StateQueryButton"
                      type="Submit"
                      value="Submit"
                      causesvalidation="False"
                      onserverclick="SubmitButton_Click"
                      runat="server"/>
            </td>
         </tr>

      </table>

      <br /><br />

      <span id="Message"
            runat="Server"/>


   </form>
</body>
</html>

備註

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

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

這個屬性通常用於事件的事件處理常式 ServerClick 中,以防止按一下 [取消] 或 [重設] 按鈕時發生頁面驗證。

適用於

另請參閱