gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:41:08 24 4
gpt4 key购买 nike

假设我有一个在 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 %>'/>

这是行不通的……有什么更正吗?谢谢之前。顺便说一句,一些例子会很棒。

更新:带有调用 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>

用户控制文件:

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

最佳答案

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

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

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

关于c# - 如何设置 RequiredFieldValidator 以针对具有多个实例的特定用户控件进行验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16983399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com