ASP.NET 中的控件
<asp:TextBox ID="txtEnd" runat="server" placeholder="12:59"></asp:TextBox>
<asp:RadioButtonList ID="rblTime2" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
<asp:ListItem>AM</asp:ListItem>
<asp:ListItem>PM</asp:ListItem>
</asp:RadioButtonList>
自定义验证器
<asp:CustomValidator ID="ValidateStartTime" ControlToValidate="txtEnd" OnServerValidate="ValidateStartTimeFun" runat="server" ErrorMessage="*required"></asp:CustomValidator>
隐藏代码
protected void ValidateStartTimeFun(object source, ServerValidateEventArgs args)
{
try
{ if (txtStart.Text != "" && rblTime.SelectedValue != null )
{ args.IsValid = true; }}
catch (Exception ex)
{ args.IsValid = false; }
}
如果我将整个 CodeBehind 更改为此,它甚至不会给我一个 *required;
protected void ValidateStartTimeFun(object source, ServerValidateEventArgs args)
{
args.isValid = false;
}
最佳答案
如果您要验证空输入,并且设置 ControlToValidate
属性且未将 ValidateEmptyText
属性设置为 true,则自定义验证器不会触发
,否则框架希望您使用 RequiredFieldValidator
。
关于c# - 为什么我的 ASP.NET 4.0 中的 CustomValidator 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14142174/