Szerkesztés

Megosztás a következőn keresztül:


PostBackOptions.PerformValidation Property

Definition

Gets or sets a value indicating whether client-side validation is required before the postback event occurs.

public:
 property bool PerformValidation { bool get(); void set(bool value); };
public bool PerformValidation { get; set; }
member this.PerformValidation : bool with get, set
Public Property PerformValidation As Boolean

Property Value

true if client-side validation is required before the postback event occurs; otherwise, false. The default value is false.

Examples

The following code example demonstrates using the ValidationGroup and PerformValidation properties to ensure that two TextBox controls have information entered in them before the postback event is generated. The NameTextBox and AgeTextBox controls are part of the PersonalInfoGroup validation group, but the StateTextBox control is not. Therefore, the user does not have to enter any data in the StateTextBox control before the postback event is generated.

Important

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

<%@ 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>

Remarks

The PerformValidation property is used to specify whether client-side validation is required before the postback event can occur. When the value of the PerformValidation property is set to true, you can also use the ValidationGroup property to specify the name of the validation group for which validation is required before the postback event occurs.

Applies to

See also