Share via


HtmlButton.CausesValidation Propiedad

Definición

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

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

Valor de propiedad

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

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.


<%@ 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>
    <title> HtmlButton CausesValidation Example </title>
<script runat="server">

      void SubmitButton_Click(Object sender, EventArgs e)
      {
         
         // Determine which button was clicked.
         switch(((HtmlButton)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>

</head>

<body>

   <form id="form1" runat="server">

      <h3> HtmlButton 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">
               <button id="CityQueryButton"
                       causesvalidation="False"
                       onserverclick="SubmitButton_Click"
                       runat="server">
                  Submit
               </button>
            </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">
               <button id="StateQueryButton"
                       causesvalidation="False"
                       onserverclick="SubmitButton_Click"
                       runat="server">
                  Submit
               </button>
            </td>
         </tr>

      </table>

      <br /><br />

      <span 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>
    <title> HtmlButton CausesValidation Example </title>
<script runat="server">

      Sub SubmitButton_Click(sender As Object, e As EventArgs)
         
         ' Determine which button was clicked.
         Select Case (CType(sender, HtmlButton)).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>

</head>

<body>

   <form id="form1" runat="server">

      <h3> HtmlButton 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">
               <button id="CityQueryButton"
                       causesvalidation="False"
                       onserverclick="SubmitButton_Click"
                       runat="server">
                  Submit
               </button>
            </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">
               <button id="StateQueryButton"
                       causesvalidation="False"
                       onserverclick="SubmitButton_Click"
                       runat="server">
                  Submit
               </button>
            </td>
         </tr>

      </table>

      <br /><br />

      <span 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 HtmlButton 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 HtmlButton control mediante la CausesValidation propiedad . Para evitar que se realice la validación, establezca la CausesValidation propiedad falseen .

Esta propiedad se usa normalmente en el controlador de eventos para el ServerClick evento para evitar que se produzca la validación de página cuando se hace clic en un botón Cancelar o Restablecer.

Se aplica a

Consulte también