HtmlInputImage.ValidationGroup 属性

定义

获取或设置在 HtmlInputImage 控件回发到服务器时导致验证的控件组。Gets or sets the group of controls for which the HtmlInputImage control causes validation when it posts back to the server.

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

属性值

HtmlInputImage 控件回发到服务器时导致验证的控件组。The group of controls for which the HtmlInputImage control causes validation when it posts back to the server. 默认值为空字符串 (""),指示尚未设置此属性。The default value is an empty string (""), indicating that this property is not set.

示例

下面的代码示例演示如何使用 ValidationGroup 属性指定 HtmlInputImage 控件回发到服务器时要验证的控件。The following code example demonstrates how to use the ValidationGroup property to specify the controls to validate when an HtmlInputImage control posts back to the server. 此页包含两个文本框,用于捕获用户的数据和两个 RequiredFieldValidator 控件,以确保用户不会将文本框留空。The page contains two text boxes to capture data from the user and two RequiredFieldValidator controls to ensure that the user does not leave a text box blank. 第一个文本框的 RequiredFieldValidator 控件位于 CityInfoGroup 验证组中,第二个框的 RequiredFieldValidator 控件位于 StateInfoGroup 验证组中。The RequiredFieldValidator control for the first text box is in the CityInfoGroup validation group and the RequiredFieldValidator control for the second box is in the StateInfoGroup validation group. 单击 CityQueryButton 后,只验证 CityInfoGroup 验证组中的控件。When the CityQueryButton is clicked, only the control in the CityInfoGroup validation group is validated. 单击 StateQueryButton 后,只验证 StateInfoGroup 验证组中的控件。When the StateQueryButton is clicked, only the control in the StateInfoGroup validation group is validated.


<%@ 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">
<script runat="server">

  void SubmitButton_Click(Object sender, ImageClickEventArgs e)
  {

    // Determine which button was clicked.
    switch (((HtmlInputImage)sender).ID)
    {

      case "CityQueryButton":

        // 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":

        // 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>

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head>
    <title> HtmlInputImage CausesValidation Example </title>
</head>

<body>

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

      <h3> HtmlInputImage 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="<br />Please enter a city."
                      Display="Dynamic"
                      EnableClientScript="False"
                      runat="server"/>
            </td>
            <td valign="bottom">
               <input id="CityQueryButton"
                      alt="City Submit button"
                      type="Image"
                      src="Image.jpg"
                      causesvalidation="true"
                      validationgroup="CityInfoGroup"
                      onserverclick="SubmitButton_Click"
                      runat="server"/>
            </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="<br />Please enter a state."
                      Display="Dynamic"
                      EnableClientScript="False"
                      runat="server"/>
            </td>
            <td valign="bottom">
               <input id="StateQueryButton"
                      alt="State Submit button"
                      type="Image"
                      src="Image.jpg"
                      causesvalidation="True"
                      validationgroup="StateInfoGroup"
                      onserverclick="SubmitButton_Click"
                      runat="server"/>
            </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">
<script runat="server">

  Sub SubmitButton_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
         
    ' Determine which button was clicked.
    Select Case (CType(sender, HtmlInputImage)).ID

      Case "CityQueryButton"

        ' 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"

        ' 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>

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head>
    <title> HtmlInputImage CausesValidation Example </title>
</head>

<body>

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

      <h3> HtmlInputImage 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="<br />Please enter a city."
                      Display="Dynamic"
                      EnableClientScript="False"
                      runat="server"/>
            </td>
            <td valign="bottom">
               <input id="CityQueryButton"
                      alt="City Submit button"
                      type="Image"
                      src="Image.jpg"
                      causesvalidation="True"
                      validationgroup="CityInfoGroup"
                      onserverclick="SubmitButton_Click"
                      runat="server"/>
            </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="<br />Please enter a state."
                      Display="Dynamic"
                      EnableClientScript="False"
                      runat="server"/>
            </td>
            <td valign="bottom">
               <input id="StateQueryButton"
                      alt="State Submit button"
                      type="Image"
                      src="Image.jpg"
                      causesvalidation="True"
                      validationgroup="StateInfoGroup"
                      onserverclick="SubmitButton_Click"
                      runat="server"/>
            </td>
         </tr>

      </table>

      <br /><br />

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


   </form>

</body>
</html>

注解

使用验证组可以将页上的验证控件分配给特定类别。Validation groups allow you to assign validation controls on a page to a specific category. 每个验证组可以独立于页面上的其他验证组进行验证。Each validation group can be validated independently from other validation groups on the page. 使用 ValidationGroup 属性可指定 HtmlInputImage 控件回发到服务器时所导致验证的验证组的名称。Use the ValidationGroup property to specify the name of the validation group for which the HtmlInputImage control causes validation when it posts back to the server.

仅当 CausesValidation 属性的值设置为 true时,此属性才有效。This property has an effect only when the value of the CausesValidation property is set to true. 如果为 ValidationGroup 属性指定值,则当 HtmlInputImage 控件回发到服务器时,只会验证属于指定组的验证控件。When you specify a value for the ValidationGroup property, only the validation controls that are part of the specified group are validated when the HtmlInputImage control posts back to the server. 如果未指定此属性的值,并且 CausesValidation 属性设置为 true,则当控件回发到服务器时,将验证该页上未分配给验证组的所有验证控件。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.

适用于

另请参阅