我想使用customvalidator控件来处理我的所有验证,但我无法弄清楚如何在代码隐藏中为不同的检查设置错误消息。这可能吗?
答案 0 :(得分:13)
您可以根据验证逻辑在OnServerValidate方法中设置错误消息:
protected void customValidator1_Validate(object sender, ServerValidateEventArgs e)
{
if (e.Value.Length < 5)
{
e.IsValid = true;
}
else
{
customValidator1.ErrorMessage = "Length must be less than 5.";
e.IsValid = false;
}
}
答案 1 :(得分:0)
对于One Control,您可以这样做..
<!-- In Designer Page -->
<asp:CustomValidator runat="server" id="cusCustom" controltovalidate="txtCustom"
onservervalidate="cusCustom_ServerValidate"
errormessage="The text must be exactly 8 characters long!" />
<br /><br />
/* In Code Behind*/
protected void cusCustom_ServerValidate(object sender, ServerValidateEventArgs e)
{
if(e.Value.Length == 8)
e.IsValid = true;
else
e.IsValid = false;
}