This documentation is archived and is not being maintained.

ButtonFieldBase.CausesValidation Property

Gets or sets a value indicating whether validation is performed when a button in a ButtonFieldBase object is clicked.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public virtual bool CausesValidation { get; set; }

Property Value

Type: System.Boolean
true to perform validation when a button in a ButtonFieldBase is clicked; otherwise, false. The default is false.

Validation groups allow you to assign validation controls on a page to a specific category. Each validation group can be validated independently of other validation groups on the page. Use the ValidationGroup property to specify the name of the validation group for which the ButtonFieldBase object causes validation when it posts back to the server.

The ValidationGroup property has an effect only when the value of the CausesValidation property is set to true. When you specify a value for the ValidationGroup property, only the validation controls that are part of the specified group are validated when the ButtonFieldBase posts back to the server. If you do not specify a value for the ValidationGroup property and the CausesValidation property is set to true, all validation controls on the page that are not assigned to a validation group are validated when the ButtonFieldBase posts back to the server.

The value of this property is stored in view state.

The following code example demonstrates how to use the CausesValidation property to perform validation when the button is clicked in the ButtonField object of a GridView control.

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

<script runat="server">

  void AuthorsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)

    // If multiple ButtonField columns are used, use the
    // CommandName property to determine which button was clicked.

      case "Edit":
        AuthorsGridView.Columns[0].Visible = false;
        AuthorsGridView.Columns[1].Visible = true;
      case "Update":
        AuthorsGridView.Columns[0].Visible = true;
        AuthorsGridView.Columns[1].Visible = false;
        // Do nothing.


  void AuthorsGridView_RowUpdating (Object sender, GridViewUpdateEventArgs e)

    // Retrieve the row being edited.    
    int index = AuthorsGridView.EditIndex;
    GridViewRow row = AuthorsGridView.Rows[index];

    // Retrieve the new value for the author's first name from the row. 
    // In this example, the author's first name is in the second cell 
    // of the row (index 1). To get the value, first retrieve the TextBox
    // that contains the value.
    TextBox firstNameTextBox = (TextBox)row.Cells[1].FindControl("FirstNameTextBox");

    // Make sure the control was found. 
    String firstName = "";    
    if(firstNameTextBox != null)
      firstName = firstNameTextBox.Text;

    // Retrieve the new value for the author's last name from the row. 
    // In this example, the author's last name is in the third cell 
    // of the row (index 2).
    TextBox lastNameTextBox = (TextBox)row.Cells[2].FindControl("LastNameTextBox");

    String lastName = "";
    if(lastNameTextBox != null)
      lastName = lastNameTextBox.Text;

    // Because custom TemplateField field columns are used, parameters 
    // are not automatically created and passed to the data source control.
    // Create Parameter objects to represent the fields to update and 
    // add the Parameter objects to the UpdateParameters collection.
    Parameter lastNameParameter = new Parameter("au_lname", TypeCode.String, lastName);   
    Parameter firstNameParameter = new Parameter("au_fname", TypeCode.String, firstName);

    // Clear the UpdateParameters collection before adding the 
    // Parameter objects. Otherwise, there will be duplicate
    // parameters.



<head runat="server">
    <title>ButtonFieldBase CausesValidation Example</title>
  <form id="form1" runat="server">

    <h3>ButtonFieldBase CausesValidation Example</h3>

    <!-- Populate the Columns collection declaratively. -->
    <asp:gridview id="AuthorsGridView" 

        <asp:buttonfield commandname="Edit"
          headertext="Edit Author">

        <asp:buttonfield commandname="Update" 
          headertext="Update Author">

        <asp:templatefield headertext="Last Name">
            <%# Eval("au_lname") %>

            <asp:textbox id="LastNameTextBox"
              text='<%# Eval("au_lname") %>'
              runat="server" /><br />  

            <asp:RequiredFieldValidator ID="LastNameRequiredValidator"
              ErrorMessage="Please enter a last name."
              Runat="server" />


        <asp:templatefield headertext="First Name">
            <%# Eval("au_fname") %>

            <asp:textbox id="FirstNameTextBox"
              text='<%# Eval("au_fname") %>'
              runat="server" /><br />  

            <asp:RequiredFieldValidator ID="FirstNameRequiredValidator"
              ErrorMessage="Please enter a first name."
              Runat="server" />




    <!-- This example uses Microsoft SQL Server and connects -->
    <!-- to the Pubs sample database.                        -->
    <asp:sqldatasource id="AuthorsSqlDataSource"  
      selectcommand="SELECT [au_id], [au_lname], [au_fname] FROM [authors]"
      updatecommand="UPDATE authors SET au_lname=@au_lname, au_fname=@au_fname WHERE (authors.au_id = @au_id)"
      connectionstring="server=localhost;database=pubs;integrated security=SSPI"


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.