Button.CausesValidation Property
Gets or sets a value indicating whether validation is performed when the Button 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 Button control is clicked; otherwise, false. The default value is true.
Remarks
By default, page validation is performed when a Button 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 a Button control is clicked by using the CausesValidation property. To prevent validation from being performed, set the CausesValidation property to false.
This property is commonly set to false for a reset or clear button to prevent validation from being performed when the 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, Button)).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 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"> <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> [C#] <%@ Page Language="C#" AutoEventWireup="True" %> <html> <head> <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 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"> <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>
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family
See Also
Button Class | Button Members | System.Web.UI.WebControls Namespace | Page.Validate