我有一个要求,多个字段中的一个是必需的。使用自定义验证器时,将返回even fires,false,但不显示任何错误消息,并验证表单。
我遗漏了什么?我尝试过使用和不使用ValidationSummary。
谢谢!
<asp:CustomValidator ID="CustomValidator1" OnServerValidate="validatePhone" EnableClientScript="false" runat="server" ErrorMessage="Home or Cell Phone is Required" ></asp:CustomValidator>
<asp:ValidationSummary ID="ValidationSummary1" DisplayMode="BulletList" runat="server" ForeColor="Red" Font-Size="X-Small" Font-Bold="true" />
protected void validatePhone(object sender, ServerValidateEventArgs e)
{
e.IsValid = string.IsNullOrEmpty(txtCellPhone.Text) && string.IsNullOrEmpty(txtHomePhone.Text) ? false : true;
}发布于 2010-11-04 23:49:56
问题完全是我的错。在提交按钮上,我要做的最后一件事是创建一个Response.Redirect。消息马上就要出来了,但随后出现了感谢页面。现在,只有当定制验证器返回true时,才执行Response.Redirect。
发布于 2010-11-04 06:54:28
您必须将ControlToValidate设置为某个TextBox。
<asp:CustomValidator ID="CustomValidator1" OnServerValidate="validatePhone" EnabEnableClientScript="false" runat="server" ErrorMessage="Home or Cell Phone is Required" ControlToValidate="txtHomePhone"/>发布于 2010-11-04 08:57:03
查看this article。基本上,您需要连接客户端验证。在关闭窗体标记前添加以下内容,根据需要更改控件名称:
<%-- This configures the validator to automatically--%>
<%-- update when either of these controls is changed --%>
<script type="text/javascript">
<!--
ValidatorHookupControlID("<%= MyControl1.ClientID %>",
document.getElementById["<%= CustomValidator1.ClientID %>"]);
ValidatorHookupControlID("<%= MyControl2.ClientID %>",
document.getElementById["<%= CustomValidator1.ClientID %>"]);
//-->
</script>或者使用this control
https://stackoverflow.com/questions/4092366
复制相似问题