HtmlButton.ValidationGroup Propiedad

Definición

Obtiene o establece el grupo de controles para los que el control HtmlButton ejecuta la validación cuando realiza devoluciones al servidor.

public:
 virtual property System::String ^ ValidationGroup { System::String ^ get(); void set(System::String ^ value); };
public virtual string ValidationGroup { get; set; }
member this.ValidationGroup : string with get, set
Public Overridable Property ValidationGroup As String

Valor de propiedad

String

Grupo de controles para los que el control HtmlButton ejecuta la validación cuando realiza devoluciones al servidor. El valor predeterminado es una cadena vacía (""), que indica que no se ha establecido esta propiedad.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar la ValidationGroup propiedad para especificar los controles que se van a validar cuando un HtmlButton control vuelve a exponer al servidor. La página contiene dos cuadros de texto para capturar datos del usuario y dos RequiredFieldValidator controles para asegurarse de que el usuario no deja un cuadro de texto en blanco. El RequiredFieldValidator control del primer cuadro de texto está en el CityInfoGroup grupo de validación y el RequiredFieldValidator control del segundo cuadro está en el StateInfoGroup grupo de validación. Cuando se hace clic en , CityQueryButton solo se valida el control del CityInfoGroup grupo de validación. Cuando se hace clic en , StateQueryButton solo se valida el control del StateInfoGroup grupo de validación.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void SubmitButton_Click(Object sender, EventArgs e)
  {
    // Determine which button was clicked.
    switch (((HtmlButton)sender).ID)
    {
        
      case "CityQueryButton":

          if (CityReqValidator.IsValid)
          {
              // Indicate that the city query was selected.
              Message.InnerHtml = "You have chosen to run a query for the following city: " +
                                  CityTextBox.Value;
          }
        
        break;
        
      case "StateQueryButton":

          if (StateReqValidator.IsValid)
          {
              // Indicate that the state query was selected.
              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>

<html xmlns="http://www.w3.org/1999/xhtml"  >

<head runat="server">
    <title>HtmlButton ValidationGroup Example</title>
</head>
<body>
    <form id="form1" runat="server">    
    
        <h3>HtmlButton ValidationGroup 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" 
                      ValidationGroup="CityInfoGroup" 
                      ErrorMessage="Please enter a city."
                      Display="Dynamic"
                      EnableClientScript="False"
                      runat="server"/>
                    
            </td>
            <td valign="bottom">
            
               <button id="CityQueryButton"
                       causesvalidation="True" 
                       validationgroup="CityInfoGroup" 
                       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" 
                      ValidationGroup="StateInfoGroup"
                      ErrorMessage="Please enter a state."
                      Display="Dynamic"
                      EnableClientScript="False"
                      runat="server"/>
                      
            </td>
            <td valign="bottom">
            
               <button id="StateQueryButton"
                       causesvalidation="True"
                       validationgroup="StateInfoGroup"
                       onserverclick="SubmitButton_Click"
                       runat="server">
                       Submit
               </button>
               
            </td>
         </tr>

      </table>

      <br /><br />

      <span id="Message"
            runat="Server"/>
    </form>
    
</body>

</html>
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  Sub SubmitButton_Click(ByVal sender As Object, ByVal e As EventArgs)
    
    ' Determine which button was clicked.
    Select Case (CType(sender, HtmlButton)).ID
      
      Case "CityQueryButton"
        
                If (CityReqValidator.IsValid) Then
                    ' Indicate that the city query was selected.
                    Message.InnerHtml = "You have chosen to run a query for the following city: " & _
                                        CityTextBox.Value
                End If
                
            Case "StateQueryButton"
        
                If (StateReqValidator.IsValid) Then
                    ' Indicate that the state query was selected.
                    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>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">
    <title> HtmlButton CausesValidation Example </title>
</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"
                      ValidationGroup="CityInfoGroup"
                      ErrorMessage="Please enter a city."
                      Display="Dynamic"
                      EnableClientScript="False"
                      runat="server"/>
            </td>
            <td valign="bottom">
            
               <button id="CityQueryButton"
                       causesvalidation="True" 
                       validationgroup="CityInfoGroup"
                       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" 
                      ValidationGroup="StateInfoGroup"
                      ErrorMessage="Please enter a state."
                      Display="Dynamic"
                      EnableClientScript="False"
                      runat="server"/>
                      
            </td>
            <td valign="bottom">
            
               <button id="StateQueryButton"
                       causesvalidation="True"
                       validationgroup="StateInfoGroup"
                       onserverclick="SubmitButton_Click"
                       runat="server">
                       Submit
               </button>
               
            </td>
         </tr>

      </table>

      <br /><br />

      <span id="Message"
            runat="Server"/>
    </form>
    
</body>

</html>

Comentarios

Los grupos de validación permiten asignar controles de validación en una página a una categoría específica. Cada grupo de validación se puede validar independientemente de otros grupos de validación de la página. Utilice la ValidationGroup propiedad para especificar el nombre del grupo de validación para el que el control hace que la HtmlButton validación se publique de nuevo en el servidor.

Esta propiedad solo tiene un efecto cuando el valor de la CausesValidation propiedad se establece trueen . Cuando se especifica un valor para la ValidationGroup propiedad , solo se validan los controles de validación que forman parte del grupo especificado cuando el HtmlButton control vuelve a realizar la publicación en el servidor. Si no especifica un valor para esta propiedad y la CausesValidation propiedad se establece trueen , todos los controles de validación de la página que no están asignados a un grupo de validación se validan cuando el control vuelve a realizar la publicación en el servidor.

Se aplica a

Consulte también