This documentation is archived and is not being maintained.

HtmlInputButton.CausesValidation Property

Gets or sets a value indicating whether validation is performed when the HtmlInputButton control is clicked.

[Visual Basic]
Public Property CausesValidation As Boolean
[C#]
public bool CausesValidation {get; set;}
[C++]
public: __property bool get_CausesValidation();
public: __property void set_CausesValidation(bool);
[JScript]
public function get CausesValidation() : Boolean;
public function set CausesValidation(Boolean);

Property Value

true if validation is performed when the HtmlInputButton control is clicked; otherwise, false. The default value is true.

Remarks

By default, page validation is performed when an HtmlInputButton control is clicked. Page validation determines whether the input controls associated with a validation control on the page all pass the validation rules specified by the validation control.

You can specify or determine whether validation is performed on both the client and the server when an HtmlInputButton control is clicked by using the CausesValidation property. To prevent validation from being performed, set the CausesValidation property to false.

This property is commonly used in the event handler for the ServerClick event to prevent page validation from occurring when a Cancel or Reset button is clicked.

Example

[Visual Basic, C#] The following example demonstrates how to use the CausesValidation property to prevent page validation from occurring. Notice how the Validate method activates each validation control independently.

[Visual Basic] 

<%@ Page Language="VB" AutoEventWireup="True" %>

<html> 

<head>

   <script runat="server">

      Sub SubmitButton_Click(sender As Object, 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 isn't recognized, erase the message on the page.
               Message.InnerHtml = ""

         End Select
        
      End Sub

   </script>

</head>

<body>

   <form 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>

[C#] 

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

<html> 

<head>

   <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 isn't recognized, erase the message on the page.
               Message.InnerHtml = "";

               break;

         }
        
      }

   </script>

</head>

<body>

   <form 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>

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

HtmlInputButton Class | HtmlInputButton Members | System.Web.UI.HtmlControls Namespace | Page.Validate | ServerClick

Show: