Muokkaa

Jaa


LinkButton.ValidationGroup Property

Definition

Gets or sets the group of controls for which the LinkButton control causes validation when it posts back to the server.

public:
 virtual property System::String ^ ValidationGroup { System::String ^ get(); void set(System::String ^ value); };
[System.Web.UI.Themeable(false)]
public virtual string ValidationGroup { get; set; }
[<System.Web.UI.Themeable(false)>]
member this.ValidationGroup : string with get, set
Public Overridable Property ValidationGroup As String

Property Value

The group of controls for which the LinkButton control causes validation when it posts back to the server. The default value is an empty string ("").

Implements

Attributes

Examples

The following code example demonstrates how to use the ValidationGroup property to specify the controls that are to be validated when a LinkButton control posts back to the server. The page contains three text boxes to capture data from the user and three RequiredFieldValidator controls to ensure that the user does not leave a text box blank. The RequiredFieldValidator controls for the first two text boxes are in the PersonalInfoGroup validation group and the RequiredFieldValidator control for the third text box is in the LocationInfoGroup validation group. When LinkButton1 is clicked, only the controls in validation group PersonalInfoGroup are validated. When LinkButton2 is clicked, only the control in validation group LocationInfoGroup is validated.

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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="head1" runat="server">
  <title>LinkButton.ValidationGroup Example</title>
</head>
<body>
  <form id="form1" runat="server">
  
    <h3>LinkButton.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 />

    <!--When LinkButton1 is clicked, only validation
    controls that are a part of PersonalInfoGroup
    are validated.-->
    <asp:linkbutton id="LinkButton1" 
      text="Validate" 
      causesvalidation="true"
      validationgroup="PersonalInfoGroup"
      runat="Server">
    </asp:linkbutton>
      
    <br /><br />
      
    <asp:label id="CityLabel" 
      text="Enter your city of residence:"
      runat="Server"
      AssociatedControlID="CityTextBox">
    </asp:label>

    &nbsp
    
    <asp:textbox id="CityTextBox" 
      runat="Server">
    </asp:textbox>

    &nbsp

    <asp:requiredfieldvalidator id="RequiredFieldValidator3"
      controltovalidate="CityTextBox"
      validationgroup="LocationInfoGroup"
      errormessage="Enter a city name."
      runat="Server">
    </asp:requiredfieldvalidator>
    
    <br /><br />

    <!--When LinkButton2 is clicked, only validation
    controls that are a part of LocationInfoGroup
    are validated.-->
    <asp:linkbutton id="LinkButton2" 
      text="Validate" 
      causesvalidation="true"
      validationgroup="LocationInfoGroup"
      runat="Server">
    </asp:linkbutton>

  </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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="head1" runat="server">
  <title>LinkButton.ValidationGroup Example</title>
</head>
<body>
  <form id="form1" runat="server">
  
    <h3>LinkButton.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 />

    <!--When LinkButton1 is clicked, only validation
    controls that are a part of PersonalInfoGroup
    are validated.-->
    <asp:linkbutton id="LinkButton1" 
      text="Validate" 
      causesvalidation="true"
      validationgroup="PersonalInfoGroup"
      runat="Server">
    </asp:linkbutton>
      
    <br /><br />
      
    <asp:label id="CityLabel" 
      text="Enter your city of residence:"
      runat="Server"
      AssociatedControlID="CityTextBox">
    </asp:label>

    &nbsp
    
    <asp:textbox id="CityTextBox" 
      runat="Server">
    </asp:textbox>

    &nbsp

    <asp:requiredfieldvalidator id="RequiredFieldValidator3"
      controltovalidate="CityTextBox"
      validationgroup="LocationInfoGroup"
      errormessage="Enter a city name."
      runat="Server">
    </asp:requiredfieldvalidator>
    
    <br /><br />

    <!--When LinkButton2 is clicked, only validation
    controls that are a part of LocationInfoGroup
    are validated.-->
    <asp:linkbutton id="LinkButton2" 
      text="Validate" 
      causesvalidation="true"
      validationgroup="LocationInfoGroup"
      runat="Server">
    </asp:linkbutton>

  </form>
</body>
</html>

Remarks

Validation groups allow you to assign validation controls on a page to a specific category. Each validation group can be validated independently of other validation groups on the page. Use the ValidationGroup property to specify the name of the validation group for which the LinkButton control causes validation when it posts back to the server.

This property has an effect only when the value of the CausesValidation property is set to true. When you specify a value for the ValidationGroup property, only the validation controls that are part of the specified group are validated when the LinkButton control posts back to the server. If you do not specify a value for this property and the CausesValidation property is set to true, all validation controls on the page that are not assigned to a validation group are validated when the control posts back to the server.

This property cannot be set by themes or style sheet themes. For more information, see ThemeableAttribute and ASP.NET Themes and Skins.

Applies to

See also