2

所以假设我有一个在 aspx 文件中调用 7 次的用户控件,我将字段验证器控件放在该用户控件中,问题是每次我单击一些导致回发的按钮时,字段验证器总是针对我的用户的所有实例验证控件控制,我想要的是针对特定的用户控制进行验证,我试过这个:

<asp:TextBox runat="server" ID="DateNew" CssClass="time" Width="185px"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Date can not be empty" ControlToValidate="DateNew" ValidationGroup='<%= ClientID %>' Text="*"> </asp:RequiredFieldValidator> 
 <asp:Button runat="server" ID="NewSchedule" Text="Schedule" CssClass="bgrey" 
        OnClientClick="CheckKeyValues()" OnClick="NewSchedule_Click"  ValidationGroup='<%= ClientId %>'/>

这不起作用...任何更正?谢谢之前。顺便说一句,一些例子会很棒。

更新:带有用户控件的 aspx 文件调用了 7 次

 <asp:ScriptManager id="ScriptManager1" runat="server">
                        </asp:ScriptManager>
                        <div id="divError" class="n fail" style="display:none; margin:15px; width:2330px">
                             <h5>
                                The following errors were found
                             </h5>
                             <asp:ValidationSummary ID="ValidationSummary" runat="server" />
                        </div>    
                        <div class="warea clearfix" style="width: 2350px">
                            <div style="float:left;">
                                <uc1:ucApplicationProgress runat="server" id="ucApplicationProgress" TestType="Interview 1" />
                            </div>
                             <div style="float:left;">
                                <uc1:ucApplicationProgress runat="server" id="ucApplicationProgress1" TestType="Interview 2"/>
                            </div>
                             <div style="float:left;">
                               <uc1:ucApplicationProgress runat="server" id="ucApplicationProgress2" TestType="Interview 3"/>
                            </div>
                             <div style="float:left;">
                                <uc1:ucApplicationProgress runat="server" id="ucApplicationProgress3" TestType="English Test"/>
                            </div>
                             <div style="float:left;">
                                <uc1:ucApplicationProgress runat="server" id="ucApplicationProgress4" TestType="Medical Examination"/>
                            </div>
                             <div style="float:left;">
                                <uc1:ucApplicationProgress runat="server" id="ucApplicationProgress5" TestType="Internal Psycho Test"/>
                            </div>
                             <div style="float:left;">
                                <uc1:ucApplicationProgress runat="server" id="ucApplicationProgress6" TestType="External Psycho Test"/>
                            </div>
                        </div>

用户控制文件:

 <h3><asp:Label runat="server" ID="TypeNew"/></h3>
              <table>
                  <tr>
                     <td>Date</td>
                     <td>
                         <asp:TextBox runat="server" ID="DateNew" CssClass="time" Width="185px"/>
                         <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Date can not be empty" ControlToValidate="DateNew">*</asp:RequiredFieldValidator>
                     </td>
                  </tr>
                  <tr>
                      <td>Time</td>
                      <td>
                          <asp:TextBox runat="server" ID="HourNew"  Width="40px"/> 
                          <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Time can not be empty" ControlToValidate="HourNew" Text="*">
                          </asp:RequiredFieldValidator>:  
                          <asp:TextBox runat="server" ID="MinuteNew"  Width="40px"/>
                          <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Minute can not be empty" ControlToValidate="MinuteNew" Text="*">
                          </asp:RequiredFieldValidator>
                      </td>
                  </tr>
                  <tr>
                      <td>Location</td>
                      <td>
                          <asp:TextBox runat="server" ID="LocationNew" TextMode="MultiLine"/>
                          <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Location can not be empty" ControlToValidate="LocationNew" Text="*">
                          </asp:RequiredFieldValidator>
                      </td>
                  </tr>
                  <tr>
                      <td>Interviewer</td>
                      <td>
                          <asp:TextBox runat="server" ID="InterviewerNew" autocomplete="off" onchange="MarkFlag(this.id)"/>
                          <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="Interviewer can not be empty" ControlToValidate="InterviewerNew" Text="*">
                          </asp:RequiredFieldValidator>
                          <asp:HiddenField runat="server" ID="InterviewerKeys"/>
                          <asp:HiddenField runat="server" ID="InterviewerEmail"/>    
                      </td>
                  </tr>
                  <tr>
                      <td>&nbsp;</td>
                      <td><asp:Button runat="server" ID="NewSchedule" Text="Schedule" CssClass="bgrey" OnClientClick="CheckKeyValues()" OnClick="NewSchedule_Click" /></td>
                  </tr>
              </table>

C# :

 public String ValidationGroup
        {
            get 
            { 
                return RequiredFieldValidator1.ValidationGroup;
            }
            set
            {
                RequiredFieldValidator1.ValidationGroup = value;
                RequiredFieldValidator2.ValidationGroup = value;
                RequiredFieldValidator3.ValidationGroup = value;
                RequiredFieldValidator4.ValidationGroup = value;
                RequiredFieldValidator5.ValidationGroup = value;
                NewSchedule.ValidationGroup = value;
            }
        }


  protected void Page_Load(object sender, EventArgs e)
        {

                ValidationGroup = this.ID;
        }
4

2 回答 2

1

在 Web 用户控件上公开一个名为 ValidationGroup 的属性:

public String ValidationGroup
{
    get { return RequiredFieldValidator1.ValidationGroup; }
    set { 
        RequiredFieldValidator1.ValidationGroup = value; 
        NewSchedule.ValidationGroup = value; 
    }
}

然后为您在 Web 表单上创建的每个实例将其设置为唯一值。

于 2013-06-07T13:45:12.317 回答
0

正如我给出的那样,给验证组值提供任何类似“名称”的东西。并将相同的验证组添加到要触发验证的控件和回发应进行验证的控件。

     <asp:TextBox ID="TextBox1" runat="server" ValidationGroup="Name"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
        ErrorMessage="Field value is required" ControlToValidate="TextBox1" 
        ValidationGroup="Name"></asp:RequiredFieldValidator>
    <asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="Name" />
    <asp:Button ID="Button2" runat="server" Text="Button" />

在这种情况下,仅当单击 Button1 而不是在 Button2 单击事件时才会触发 TextBox1 验证。

于 2013-06-07T12:46:18.753 回答