简体   繁体   English

如何设置RequiredFieldValidator以针对具有多个实例的特定用户控件进行验证?

[英]how to set RequiredFieldValidator to validate against specific user control which has multiple instance?

so let say i have a user control which called 7 times in an aspx file, i put field validator control in that user control, the problem is everytime i click some buttons which cause postback, field validator always validate controls against all instances of my user control, what i want is to validate against specific user control, i have tried this : 因此,假设我有一个在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 %>'/>

this is not working... any correction? 这不起作用...任何更正? thanx before. 谢谢。 btw some example would be great. 顺便说一句,很好的例子。

update : aspx file with a user control called 7 times update:带有7次用户控件的aspx文件

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

user control file : 用户控制文件:

 <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# : 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;
        }

Expose a property on your Web User Control named ValidationGroup: 在您的Web用户控件上公开一个名为ValidationGroup的属性:

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

Then set it to a unique value for each instance you create on your web form. 然后将其设置为在Web表单上创建的每个实例的唯一值。

Give the validation group value anything like "Name" as i have given. 像我给的那样给验证组值一个类似“名称”的名字。 And add the same validation group to both controls on which validation is to be fired and the control whose post back should fir validation. 并将相同的验证组添加到要触发验证的两个控件以及应发回验证的控件。

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

As in this case TextBox1 validation fires only when Button1 is clicked not on Button2 click event. 在这种情况下,仅在单击Button1而不在Button2 click事件上时才触发TextBox1验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM