gpt4 book ai didi

c# - 为什么我的 CustomValidator 在 ASP.NET 4.0 中不工作?

转载 作者:太空宇宙 更新时间:2023-11-03 11:10:43 25 4
gpt4 key购买 nike

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

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.validateemptytext.aspx

关于c# - 为什么我的 CustomValidator 在 ASP.NET 4.0 中不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14142174/

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