Button.CausesValidation Propiedad

Definición

Obtiene o establece un valor que indica si la validación se realiza al hacer clic en el control Button.

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

Valor de propiedad

Es true si se realiza la validación cuando se hace clic en el control Button; en caso contrario, es false. El valor predeterminado es true.

Implementaciones

Atributos

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar la CausesValidation propiedad para evitar que se produzca la validación de páginas. Tenga en cuenta que el Validate método activa cada control de validación de forma independiente.

Importante

Este ejemplo tiene un cuadro de texto que acepta datos proporcionados por el usuario, lo que puede suponer una amenaza para la seguridad. De forma predeterminada, ASP.NET Web Pages valida que los datos proporcionados por el usuario no incluyen elementos HTML ni de script. Para más información, consulte Información general sobre los ataques mediante scripts.


<%@ 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> Button CausesValidation Example </title>
<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 id="form1" 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>

<%@ 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> Button CausesValidation Example </title>
<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 id="form1" 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>

Comentarios

De forma predeterminada, la validación de página se realiza cuando se hace clic en un Button control. La validación de página determina si los controles de entrada asociados a un control de validación de la página pasan todas las reglas de validación especificadas por el control de validación.

Puede especificar o determinar si la validación se realiza tanto en el cliente como en el servidor cuando se hace clic en un Button control mediante la CausesValidation propiedad . Para evitar que se realice la validación, establezca la CausesValidation propiedad falseen .

Nota:

Debe establecer la CausesValidation propiedad false en cuando use la PostBackUrl propiedad para volver a publicar en otra página. Debe comprobar explícitamente la validación al volver a publicar en otra página. Para obtener un ejemplo, vea la sección Comentarios de la PostBackUrl propiedad .

Esta propiedad se establece false normalmente en para un reset botón o clear para evitar que se realice la validación cuando se hace clic en el botón.

Cuando el valor de la CausesValidation propiedad se establece trueen , también puede usar la ValidationGroup propiedad para especificar el nombre del grupo de validación para el que el control provoca la Button validación.

Esta propiedad no se puede establecer mediante temas o temas de la hoja de estilos. Para obtener más información, consulte ThemeableAttribute y ASP.NET Temas y máscaras.

Se aplica a

Consulte también