PostBackOptions.ValidationGroup Proprietà

Definizione

Ottiene o imposta il gruppo di controlli per il quale l'oggetto PostBackOptions provoca la convalida in occasione del postback al server.

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

Valore della proprietà

String

Gruppo di controlli per il quale l'oggetto PostBackOptions provoca la convalida in occasione del postback al server. Il valore predefinito è una stringa vuota ("").

Esempio

Nell'esempio di codice seguente viene illustrato l'uso delle ValidationGroup proprietà e PerformValidation per assicurarsi che due TextBox controlli dispongano di informazioni immesse prima che venga generato l'evento di postback. I NameTextBox controlli e AgeTextBox fanno parte del PersonalInfoGroup gruppo di convalida, ma il StateTextBox controllo non è . Pertanto, l'utente non deve immettere dati nel StateTextBox controllo prima che venga generato l'evento di postback.

Importante

L'esempio include una casella di testo che accetta l'input dell'utente e rappresenta quindi una potenziale minaccia alla sicurezza. Per impostazione predefinita, le pagine Web ASP.NET verificano che l'input dell'utente non includa script o elementi HTML. Per altre informazioni, vedere Cenni preliminari sugli attacchi tramite script.

<%@ 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 Page_Init(object sender, EventArgs e)
  {
    PostBackOptions myPostBackOptions = new PostBackOptions(FruitRadioButtonList);

    myPostBackOptions.RequiresJavaScriptProtocol = true;
    myPostBackOptions.PerformValidation = true;
    myPostBackOptions.ValidationGroup = "PersonalInfoGroup";

    string reference = Page.ClientScript.GetPostBackEventReference(myPostBackOptions);
    FruitRadioButtonList.Attributes.Add("onclick", reference);
  } 

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>PerformValidation and ValidationGroup Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
      <h3>PostBackOptions PerformValidation and ValidationGroup Example</h3>
      
      <asp:label id="NameLabel" 
        text="Enter your name:"
        runat="server"
        AssociatedControlID="NameTextBox">
      </asp:label>

      &nbsp
      
      <asp:textbox id="NameTextBox" 
        runat="server">
      </asp:textbox>

      &nbsp

      <asp:requiredfieldvalidator id="RequiredFieldValidator1"
        controltovalidate="NameTextBox"
        validationgroup="PersonalInfoGroup"
        errormessage="Enter your name."
        runat="server">
      </asp:requiredfieldvalidator>
      
      <br /><br />
      
      <asp:label id="AgeLabel" 
        text="Enter your age:"
        runat="server"
        AssociatedControlID="AgeTextBox">
      </asp:label>

      &nbsp
      
      <asp:textbox id="AgeTextBox" 
        runat="server">
      </asp:textbox>

      &nbsp

      <asp:requiredfieldvalidator id="RequiredFieldValidator2"
        controltovalidate="AgeTextBox"
        validationgroup="PersonalInfoGroup"
        errormessage="Enter your age."
        runat="server">
      </asp:requiredfieldvalidator>
      
      <br /><br />
      
      <asp:label id="State" 
        text="Enter the state where you live:"
        runat="server"
        AssociatedControlID="StateTextBox">
      </asp:label>

      &nbsp
      
      <asp:textbox id="StateTextBox" 
        runat="server">
      </asp:textbox>

      &nbsp
      
      <br /><br />
    
    
      <asp:Label id="FruitLabel" 
        text="Please select your preferred fruit:" 
        runat="server"
        AssociatedControlID="FruitRadioButtonList">
      </asp:Label>
      
      <asp:RadioButtonList ID="FruitRadioButtonList" runat="server" >
        <asp:ListItem>Apples</asp:ListItem>
        <asp:ListItem>Oranges</asp:ListItem>
        <asp:ListItem>Pears</asp:ListItem>
        <asp:ListItem>Peaches</asp:ListItem>
        <asp:ListItem>Grapes</asp:ListItem>
        <asp:ListItem>Lemons</asp:ListItem>
        <asp:ListItem>Limes</asp:ListItem>
        <asp:ListItem>Plums</asp:ListItem>
      </asp:RadioButtonList>
      
    </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 Page_Init(ByVal sender As Object, ByVal e As EventArgs)
  
    Dim myPostBackOptions As PostBackOptions = New PostBackOptions(FruitRadioButtonList)

    myPostBackOptions.RequiresJavaScriptProtocol = True
    myPostBackOptions.PerformValidation = True
    myPostBackOptions.ValidationGroup = "PersonalInfoGroup"

    Dim reference As String = Page.ClientScript.GetPostBackEventReference(myPostBackOptions)
    FruitRadioButtonList.Attributes.Add("onclick", reference)
    
  End Sub


</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>PerformValidation and ValidationGroup Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
      <h3>PostBackOptions PerformValidation and ValidationGroup Example</h3>
      
      <asp:label id="NameLabel" 
        text="Enter your name:"
        runat="server"
        AssociatedControlID="NameTextBox">
      </asp:label>

      &nbsp
      
      <asp:textbox id="NameTextBox" 
        runat="server">
      </asp:textbox>

      &nbsp

      <asp:requiredfieldvalidator id="RequiredFieldValidator1"
        controltovalidate="NameTextBox"
        validationgroup="PersonalInfoGroup"
        errormessage="Enter your name."
        runat="server">
      </asp:requiredfieldvalidator>
      
      <br /><br />
      
      <asp:label id="AgeLabel" 
        text="Enter your age:"
        runat="server"
        AssociatedControlID="AgeTextBox">
      </asp:label>

      &nbsp
      
      <asp:textbox id="AgeTextBox" 
        runat="server">
      </asp:textbox>

      &nbsp

      <asp:requiredfieldvalidator id="RequiredFieldValidator2"
        controltovalidate="AgeTextBox"
        validationgroup="PersonalInfoGroup"
        errormessage="Enter your age."
        runat="server">
      </asp:requiredfieldvalidator>
      
      <br /><br />
      
      <asp:label id="State" 
        text="Enter the state where you live:"
        runat="server"
        AssociatedControlID="State">
      </asp:label>

      &nbsp
      
      <asp:textbox id="StateTextBox" 
        runat="server">
      </asp:textbox>

      &nbsp
      
      <br /><br />
    
    
      <asp:Label id="FruitLabel" 
        text="Please select your preferred fruit:" 
        runat="server"
        AssociatedControlID="FruitRadioButtonList">
      </asp:Label>
      
      <asp:RadioButtonList ID="FruitRadioButtonList" runat="server" >
        <asp:ListItem>Apples</asp:ListItem>
        <asp:ListItem>Oranges</asp:ListItem>
        <asp:ListItem>Pears</asp:ListItem>
        <asp:ListItem>Peaches</asp:ListItem>
        <asp:ListItem>Grapes</asp:ListItem>
        <asp:ListItem>Lemons</asp:ListItem>
        <asp:ListItem>Limes</asp:ListItem>
        <asp:ListItem>Plums</asp:ListItem>
      </asp:RadioButtonList>
      
    </form>
  </body>
</html>

Commenti

Utilizzare la ValidationGroup proprietà per specificare il gruppo di convalida da convalidare quando viene generato un evento di postback. Vengono convalidati solo i controlli di convalida nel gruppo di convalida specificato.

Si applica a

Vedi anche