您可以制作一个标签控件并在其中设置错误消息:
在 ASPX 中:
<asp:Label Id="ErrorMessage" runat="server" CssClass="error"></asp:Label>
在后面的代码中:
Protected Sub cvInvDate_ServerValidate(source As Object, args As
ServerValidateEventArgs)
If IsDate(args.Value) Then
If Year(args.Value) < Year(Today) - 2 Or Year(args.Value) > Year(Today) + 1 Then
args.IsValid = False
Else
args.IsValid = True
End If
Else
args.IsValid = True
End If
If args.IsValid Then
ErrorMessage.Text = ""
Else
ErrorMessage.Text = "The Invoice Date must be between X and Y"
End If
End Sub
或者您可以查看ValidationSummary控件。
这里只需要设置ErrorMessage属性并插入一个新的ValidationSummary控件:
<asp:CustomValidator ID="cvInvDate" runat="server"
CssClass="error" ControlToValidate="dpInvDate"
OnServerValidate="cvInvDate_ServerValidate"
ErrorMessage="The Invoice Date must be between X and Y">*</asp:CustomValidator>
<asp:ValidationSummary
HeaderText="Error Messages:"
runat="server"/>
这也是W3C 学校的一个例子
编辑:当 CustomValidator OnServerValidate Event failed: 时args.IsValid = False
,ErrorMessage 将显示在 ValidationSummary 中。